Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 3.0新特性--》ViewFilperAdapter

android 3.0新特性--》ViewFilperAdapter

編輯:關於Android編程

最近在做一個電子相框主要能夠播放幻燈片,起初使用ViewPager但是很難讓其自動循環播放幻燈片,之後使用ViewFliper雖然能夠實現自動播放幻燈片,使用軟引用解決內存溢出的問題,但是他的效率很低,不能實現循環復用View,要特高效率還是得循環復用,android3.0新特性裡邊就有這樣成型的api、ViewFilperAdapter,個人覺得非常要用,AdapterViewFlipper 和BaseAdapter一起使用就解決了循環復用,滑到那一頁在加載那一頁的數據,真正的動態加載數據。

ObjectAnimator rInAnim, rOutAnim, rightIn, rightOut, leftIn, leftOut;//3.0以前,android支持兩種動畫模式,tween animation,frame animation,在android3.0中又引入了一 個新

的動畫系統:property animation,這三種動畫模式在SDK中被稱為property animation,view animation,drawable animation

AdapterViewFlipper iv=(AdapterViewFlipper ) findViewById(R.id.viewpager);

ViewFilperAdapter adapter = new ViewFilperAdapter(Single.getInstance().getPathList());

iv.setAdapter(adapter);

public void setViewFlipperAnim() {
if (iv.isFlipping()) {
iv.stopFlipping();
}
iv.setInAnimation(this, android.R.animator.fade_in);//加入ObjectAnimator
iv.setOutAnimation(this, android.R.animator.fade_out);

iv.setAutoStart(true); // 設置自動播放功能(點擊事件,前自動播放)
iv.setFlipInterval(3000);//設置動畫時間間隔3000毫秒
if (iv.isAutoStart() && !iv.isFlipping()) {
iv.startFlipping();
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
iv.stopFlipping();
iv.setAutoStart(false);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
return true;
case MotionEvent.ACTION_UP:
if (event.getX() > startX) { // 向右滑動
if (event.getX() - startX > 20) {
rInAnim = ObjectAnimator.ofFloat(iv, "x", iv.getX(), iv.getWidth());
rInAnim.setDuration(200);
iv.setInAnimation(rInAnim);
iv.setOutAnimation(rInAnim);
iv.showNext();
return false;
}
} else if (event.getX() < startX) { // 向左滑動
if (startX - event.getX() > 20) {
rOutAnim = ObjectAnimator.ofFloat(iv, "x", iv.getX(), -iv.getWidth());
rOutAnim.setDuration(200);
iv.setInAnimation(rOutAnim);
iv.setOutAnimation(rOutAnim);
iv.showPrevious();
return false;
}
}
break;
}
return super.onTouchEvent(event);
}


class MyHandler extends Handler {

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:
adapter.notifyDataSetChanged();
break;

}

}

class ViewFilperAdapter extends BaseAdapter {
private ArrayList imgpaths;

public ViewFilperAdapter(ArrayList imgpaths) {
// TODO Auto-generated constructor stub

this.imgpaths = imgpaths;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return imgpaths.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return imgpaths.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(MainGallery.this).inflate(
R.layout.gridview_item, null);
holder = new Holder();
holder.img = (ImageView) convertView
.findViewById(R.id.gridview_item_img);
holder.head = (ImageView) convertView.findViewById(R.id.user_img);
holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
holder.creat_time = (TextView) convertView.findViewById(R.id.creat_time);
holder.caption = (TextView) convertView.findViewById(R.id.caption);
holder.location = (TextView) convertView.findViewById(R.id.location);
holder.comment_list = (ListView) convertView.findViewById(R.id.comment_list);
convertView.setTag(holder);

} else {
holder = (Holder) convertView.getTag();
}
String[] as = imgpaths.get(position).split("/");
Img img = dbOpenHelper.getImg(as[as.length - 1]);
if (null != img&&settingSharePreferences.getComment()) {
String userHead = dbOpenHelper.getHeadImg(img.getImg_from_id());
holder.head.setImageBitmap(ImageUtil.getImageFromLocal(ImageUtil
.getCacheImgPath().concat(
"head/" + ImageUtil.md5(userHead))));
holder.user_name.setText(img.getImg_from_name());
java.util.Date dt = new Date();
int date = (int) (dt.getTime() / 1000 - img.getImg_time());
holder.creat_time.setText(getTime(date));
holder.caption.setText(img.getCaption_text());
ArrayList comments = dbOpenHelper.getComments(img
.getImg_id());
if (comments.size() != 0) {
holder.comment_list.setAdapter(new CommentAdapter(
MainGallery.this, comments));

}
}
Img imgs=ImgCache.getInstance().getBitmap(imgpaths.get(position), MainGallery.this);
holder.img.setImageBitmap(imgs.getImgBitmap());
return convertView;
}
}
class Holder {
ImageView img, head;
TextView user_name, creat_time, caption, location;
ListView comment_list;
}

anim_left_in.xml


android:duration="500"
android:propertyName="X"
android:valueFrom="1024"
android:valueTo="0"
android:valueType="floatType" />

anim_left_out.xml


android:duration="500"
android:propertyName="X"
android:valueFrom="0"
android:valueTo="-1024"
android:valueType="floatType"
/>

anim_right_in.xml
android:duration="500"
android:propertyName="X"
android:valueFrom="0"
android:valueTo="1024"
android:valueType="floatType"
/>

anim_right_out.xml

android:duration="500"
android:propertyName="X"
android:valueFrom="0"
android:valueTo="1024"
android:valueType="floatType"
/>




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved