广告

来源:互联网 发布:帝国最后的荣耀 知乎 编辑:程序博客网 时间:2024/04/24 10:33
public class ImageAdapter extends BaseAdapter{private Context context;private ArrayList<Drawable> drawables;public ImageAdapter(Context context,ArrayList<Drawable> drawables) {this.context=context;this.drawables=drawables;}@Overridepublic int getCount() {return drawables.size();}@Overridepublic Object getItem(int position) {return drawables.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView==null){convertView = new ImageView(context);((ImageView) convertView).setScaleType(ScaleType.FIT_XY);}if(drawables.size() > 0){Drawable item = drawables.get(position % drawables.size());((ImageView) convertView).setImageDrawable(item);}return convertView;}public ArrayList<Drawable> getDrawables() {return drawables;}public Context getContext() {return context;}}
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); AutoPlayGallery g = (AutoPlayGallery) findViewById(R.id.autoGallery);     ArrayList<Drawable> drawables = new ArrayList<Drawable>();     drawables.add(getResources().getDrawable(R.drawable.tuijian_001));     drawables.add(getResources().getDrawable(R.drawable.tuijian_002));     drawables.add(getResources().getDrawable(R.drawable.tuijian_003));          ImageAdapter adapter = new ImageAdapter(this, drawables);     g.setAdapter(adapter);}

package com.example.ui;public class AutoPlayGallery extends RelativeLayout {private final static String PACKAGE_NAME = "/com/autoplay/res/"; // baseprivate final static int BASE_BACKGROUND_COLOR = 0x33000000; // baseprivate int duration = 3000; // switch durationprivate MyGallery mGallery;private RadioGroup radioGroup;private Bitmap pointBg;private Bitmap pointPressedBg;private int height = 30; // base height ,can be modify by setHeightprivate boolean flag = false; // switch for playingprivate Thread autoPlayThread;public AutoPlayGallery(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setupContentView(context);}public AutoPlayGallery(Context context, AttributeSet attrs) {super(context, attrs);setupContentView(context);}public AutoPlayGallery(Context context) {super(context);setupContentView(context);}private void setupContentView(Context context) {pointBg = BitmapFactory.decodeResource(getResources(), R.drawable.point);pointPressedBg = BitmapFactory.decodeResource(getResources(), R.drawable.point_pressed);LinearLayout indicator = new LinearLayout(context);int heightPX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, getResources().getDisplayMetrics()); // change inch to pixindicator.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,heightPX));indicator.setBackgroundColor(BASE_BACKGROUND_COLOR);indicator.setGravity(Gravity.CENTER);radioGroup = new RadioGroup(context);radioGroup.setOrientation(RadioGroup.HORIZONTAL);indicator.addView(radioGroup);mGallery = new MyGallery(context);Gallery.LayoutParams paramGallery = new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,Gallery.LayoutParams.FILL_PARENT);mGallery.setLayoutParams(paramGallery);mGallery.setSpacing(1); // set the spacing between itemsmGallery.setUnselectedAlpha(1.0f); // make all the items lightmGallery.setHorizontalFadingEdgeEnabled(false); // take off the fading// effectaddView(mGallery, new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.FILL_PARENT));android.widget.RelativeLayout.LayoutParams param = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);addView(indicator, param);}/** * set adapter to start the gallery * @param adapter */public void setAdapter(ImageAdapter adapter) {ArrayList<Drawable> drawables = adapter.getDrawables();if (drawables != null && drawables.size() > 0) {setIndicator(adapter.getContext(), drawables.size());mGallery.setAdapter(adapter);mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View view,int position, long arg3) {// TODO Auto-generated method stubindicatePoint(position);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});flag = true;// make the switch trueplay(drawables.size());}}/** * stop the play thread */public void stop() {flag = false;autoPlayThread.interrupt();mGallery.setAdapter(new ImageAdapter(getContext(), new ArrayList<Drawable>()));radioGroup.removeAllViews();}public boolean isPlaying(){return flag;}public void setDuration(int duration) {this.duration = duration;}/** * set layout height *  * @param height */public void setHeight(int height) {this.height = height;}private void setIndicator(Context context, int size) {for (int i = 0; i < size; i++) {ImageView point = new ImageView(context);point.setImageResource(R.drawable.point);radioGroup.addView(point);}}private void indicateImage(int position) {mGallery.setSelection(position);imageViewInAniamtion(0);indicatePoint(position);}/** * setAnimation for auto play item *  * @param position */private void imageViewInAniamtion(int position) {ImageView img = (ImageView) mGallery.getChildAt(position);if(img!=null){img.startAnimation(getTranslateAnimation());}}/** * a tween animation *  * @return */private Animation getTranslateAnimation() {AnimationSet as = new AnimationSet(true);TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,-1.0f, Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, 0);ta.setDuration(500);as.addAnimation(ta);return as;}/** * light the specified point *  * @param position */private void indicatePoint(int position) {for (int i = 0; i < radioGroup.getChildCount(); i++) {((ImageView) radioGroup.getChildAt(i)).setImageBitmap(pointBg);}((ImageView) radioGroup.getChildAt(position)).setImageBitmap(pointPressedBg);}private int count;/** * play by a thread *  * @param size */private void play(final int size) {final Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif (mGallery.isTouched()) {count = mGallery.getFirstVisiblePosition() + 1;mGallery.setTouched(false);} else {count++;}indicateImage(count % size);}};autoPlayThread = new Thread(new Runnable() {@Overridepublic void run() {do{handler.sendEmptyMessage(0);try {Thread.sleep(duration);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();break;}}while(flag);}});autoPlayThread.start();}}class MyGallery extends Gallery {public MyGallery(Context context) {super(context);// TODO Auto-generated constructor stub}public MyGallery(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public MyGallery(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}// Convert the dips to pixelsfloat scale = getResources().getDisplayMetrics().density;int FLINGTHRESHOLD = (int) (20.0f * scale + 0.5f);int SPEED = 600;private boolean isTouched = false;public void setTouched(boolean isTouched) {this.isTouched = isTouched;}public boolean isTouched() {return isTouched;}public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {setTouched(true);if (velocityX > FLINGTHRESHOLD) {return super.onFling(e1, e2, SPEED, velocityY);} else if (velocityX < -FLINGTHRESHOLD) {return super.onFling(e1, e2, -SPEED, velocityY);} else {return super.onFling(e1, e2, velocityX, velocityY);}}}


                                             
0 0
原创粉丝点击