基于触摸坐标绘制一系列图像

时间:2013-09-11 02:12:27

标签: java android android-animation

我想从触摸事件触发中绘制一系列图像。从path内的onDraw内部,我将使用它作为绘制图像的位置的指南。 我现在的问题是显示一系列随机图像,而不仅仅是一个。然后最终显示的图像将逐渐淡出。

enter image description here

  public class SingleTouchEventView extends View {
  private Paint paint = new Paint();
  private Path path = new Path();
  float eventX;
  float eventY;
  Bitmap bmp;

  public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @SuppressLint("DrawAllocation")
@Override
  protected void onDraw(Canvas canvas) {

    /*Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_stub);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(bmp, 10, 10, null);*/
    canvas.drawPath(path, paint);


   int rand = (int) (Math.random() * 3);
    Log.d("Integer", ""+rand);
   if(rand==0){
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_stub);
   }else
    if(rand==1)
    {
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_stub);
    }
    else if(rand==2)
    {
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bg_striped_img);
    }

canvas.drawBitmap(bmp, eventX, eventY, paint);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    eventX = event.getX();
    eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      path.moveTo(eventX, eventY);
      return true;
    case MotionEvent.ACTION_MOVE:
      path.lineTo(eventX, eventY);
      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }
}

0 个答案:

没有答案