【问题标题】:Moving bitmap on canvas在画布上移动位图
【发布时间】:2013-03-31 09:49:37
【问题描述】:

我有一个 imageView、画布和一个按钮.. 当我单击按钮时,画布上会绘制一个位图

我想使用我的 onTouch 移动该位图(将位图拖到画布上的任意位置)。

       s.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> parent,
                   View v, int position, long id) {
            Bitmap workingBitmap = Bitmap.createBitmap(currentBitmap);
            workingBitmap = Bitmap.createBitmap(workingBitmap); 
            Canvas c = new Canvas(workingBitmap);
            brightBitmap = BitmapFactory.decodeResource(getResources(), sIds.mSmileyIds[position], null); 
            brightBitmap = Bitmap.createScaledBitmap(brightBitmap, 100, 100, false);
            chosenSmiley = brightBitmap;
                if (chosenSmiley != null) {
                    try {
                    c.drawBitmap(chosenSmiley, posX, posY, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
                iv.setImageBitmap(workingBitmap);
               }

        public boolean onTouchEvent(MotionEvent event) {
            int eventaction = event.getAction();

            switch (eventaction) {
                case MotionEvent.ACTION_DOWN: 
                    // finger touches the screen
                    break;

                case MotionEvent.ACTION_MOVE:
                    // finger moves on the screen
                    lastX = (int) event.getX();
                    lastY = (int) event.getY();
                    Log.e("my xname", lastX + " Coords of lastX");
                    Log.e("my xname", lastY + " Coords of lastY");
                    brightBitmap = Bitmap.createScaledBitmap(brightBitmap, lastX, lastY, false);
                    break;

                case MotionEvent.ACTION_UP:   
                    // finger leaves the screen
                    break;
            }

            // tell the system that we handled the event and no further processing is required
            return true; 
        }   

       });

这是我当前的代码,位图是在 0,0 上创建的,但我无法拖动它等等 ..

【问题讨论】:

  • 点击绘制图像时有一个按钮。然后用手指移动图像。这就是你要找的吗?

标签: android canvas bitmap


【解决方案1】:
 public class MainActivity extends Activity
{
  @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    DrawingView dv = new DrawingView(this);
    setContentView(dv);
}

class DrawingView extends View{
Bitmap bitmap;

float x,y;

public DrawingView(Context context)
{
 super(context);
 bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}


public boolean onTouchEvent(MotionEvent event)
{

 switch(event.getAction())
    {
   case MotionEvent.ACTION_DOWN: {

   }
   break;

   case MotionEvent.ACTION_MOVE: 
   {
      x=(int)event.getX();
      y=(int)event.getY();

 invalidate();
 }

 break;
   case MotionEvent.ACTION_UP: 

       x=(int)event.getX();
       y=(int)event.getY();
       System.out.println(".................."+x+"......"+y); //x= 345 y=530 
       invalidate();
    break; 
   }
  return true;
 }

 @Override
 public void onDraw(Canvas canvas)
 {
 Paint paint = new Paint();
 paint.setStyle(Paint.Style.FILL);
 paint.setColor(Color.CYAN);
 canvas.drawBitmap(bitmap, x, y, paint);  //originally bitmap draw at x=o and y=0
 }
 }
 }

星位图在x=0和y=0处绘制;在拖动 x 和 y 更改时调用 inavlidate 以刷新 drate。在 touch up 时,在拖动位置 x 和 y 绘制位图并调用 invalidate 以刷新绘制。

在 x=0 y=0 处绘制的位图。拖放时 //x= 345 y=530。生成的快照已附加。

您需要确保您的图像在屏幕边缘看起来不会超出屏幕检查 x 是否在屏幕宽度 - 位图宽度内,并且 y 是否小于屏幕高度 - 位图高度。我没有在代码中包含这些条件。

编辑:

      setContentView(R.layout.main);    
      LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
      DrawingView dv= new DrawingView(this);
      ll.addView(dv);

【讨论】:

  • 您设置视图的大小。以上取屏幕的高度和宽度。我可以拥有其他 ui 元素并最小化画布绘制空间。
  • 您可以使用 setContentView(R.layout.main); 设置布局;现在在 main.xml 中定义一个布局,比如线性布局。设置它的宽度和高度。将绘图视图添加到您的布局中。
  • 您可以用手指拖动并在释放时将位图绘制到该位置。而不是点击图像视图在画布上绘制图像。
  • 检查编辑。 main.xml 可以有按钮。设置布局。单击时调用无效,因此会调用 Drawingaviews 绘制。在 onDraw 中绘制图像。
猜你喜欢
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2018-09-05
相关资源
最近更新 更多