編輯:關於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
public ViewFilperAdapter(ArrayList
// 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
.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:propertyName="X"
android:valueFrom="1024"
android:valueTo="0"
android:valueType="floatType" />
anim_left_out.xml
android:propertyName="X"
android:valueFrom="0"
android:valueTo="-1024"
android:valueType="floatType"
/>
anim_right_in.xml
android:propertyName="X"
android:valueFrom="0"
android:valueTo="1024"
android:valueType="floatType"
/>
anim_right_out.xml
android:propertyName="X"
android:valueFrom="0"
android:valueTo="1024"
android:valueType="floatType"
/>
上一篇說到了通知欄Notification,提起通知欄,不得讓人想到Service以及BroadcastReceive,作為android的4大組建的2個重要成員,我們沒
最近沒事的時候想自己寫一個支持下拉刷新,上拉加載的自定義View。寫著寫著,就覺得最常見的“一個圈轉啊轉”的進度條太普通了。於是,就想看看有沒有更
PS:最近看到很多人都開始寫年終總結了,時間過得飛快,又到年底了,又老了一歲。學習內容:1.進度條2.縮放3.ControllerBuilder,ControllerLi
vivo X9作為此前X7的升級版,外觀變化不大,主要變化在於背面天線變了,采用類似魅族MX6/iPhone7類似的弧線天線設計,背面金屬屏占比更高,外觀顯