Hi all, I write a simple program that can slide image by finger touch.
It seems to work fine but the edge of the image looks flickered when sliding. if anyone can help me to figure out that problem? thanks in advance, Walton here is the demo on Youtube: http://www.youtube.com/watch?v=bNaJ3YGN9Co my source code: (please put three pictures(320x480) in /res/drawable, named a.jpg, b.jpg and c.jpg) ---------------------------- package com.example.android.picSlide; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; public class picSlide extends Activity { final int width = 320, height = 480; final int numPic = 3; float [] x = new float[numPic]; float y, xLast, yLast; int curIndex = 0; boolean drag = false; View myView; Bitmap [] img = new Bitmap[numPic]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,WindowManager.LayoutParams. FLAG_FULLSCREEN); getImg(); myView = new MyView(this); setContentView(myView); } public boolean onTouchEvent(MotionEvent event) { float x1 = event.getX(), y1 = event.getY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: xLast = x1; yLast = y1; drag = false; break; case MotionEvent.ACTION_UP: changeImg(); drag = true; myView.invalidate(0, 0, width, height); break; case MotionEvent.ACTION_MOVE: x[curIndex] += (x1-xLast); if (x[curIndex] < 0 && curIndex+1 <= numPic-1) //drag to left x[curIndex+1] = width + x[curIndex]; else if (x[curIndex] > 0 && curIndex-1 >= 0) //drag to right x[curIndex-1] = (-width) + x[curIndex]; xLast = x1; myView.invalidate(0, 0, width, height); break; } return super.onTouchEvent(event); } class MyView extends View{ @Override protected void onDraw(Canvas canvas) { Paint mPaint = new Paint(); //mPaint.setAntiAlias(true); slide(); canvas.drawBitmap(img[curIndex], x[curIndex], y, mPaint); if (x[curIndex] < 0 && curIndex+1 <= numPic-1) //drag to left canvas.drawBitmap(img[curIndex+1], x[curIndex+1], y, mPaint); else if (x[curIndex] > 0 && curIndex-1 >= 0) //drag to right canvas.drawBitmap(img[curIndex-1], x[curIndex-1], y, mPaint); super.onDraw(canvas); } public MyView(Context context) { super(context); } } private void changeImg() { final int factor = 6; if(x[curIndex] > width/factor && curIndex-1 >= 0){ //drag to right curIndex--; } else if(x[curIndex] < -width/factor && curIndex+1 <= numPic-1){ curIndex++; } } private void getImg() { int [] pic = {R.drawable.a, R.drawable.b, R.drawable.c}; for (int i=0; i < numPic; i++) img[i] = BitmapFactory.decodeResource(getResources(), pic[i]); } private void slide() { final float factor = 1.1f; //greater indicates move faster if (drag){ if ((int)x[curIndex] > 0){ //drag to right x[curIndex] /= factor; if (curIndex-1 >= 0) x[curIndex-1] = (-width) + x[curIndex]; } else if ((int)x[curIndex] < 0){ //drag to left x[curIndex] /= factor; if (curIndex+1 <= numPic-1) x[curIndex+1] = width + x[curIndex]; } else{ x[curIndex] = 0; drag = false; } myView.invalidate(0, 0, width, height); } } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en