在画布上移动位图 [英] Moving bitmap on canvas

查看:169
本文介绍了在画布上移动位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个imageView,画布和一个按钮..
当我点击按钮一个位图绘制在画布上

I have an imageView, canvas, and a button.. when I click the button a bitmap gets drawn on the canvas

我想移动

       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创建,但我不能拖动

this is my current code, the bitmap gets created on 0,0 but I can't drag it etc ..

推荐答案

 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。在触摸时,在拖动的位置x和y处绘制位图,并调用invalidate以刷新绘图。

At the star bitmap is draw at x=0 and y=0; On drag x and y changes so call inavlidate to refresh drate. On touch up draw the bitmap at the dragged position x and y and call invalidate to refresh draw.

在x = 0处绘制位图y = 0。 on drag and drop // x = 345 y = 530。附加所得到的快照。

Bitmap drawn at x=0 y=0. on drag and drop //x= 345 y=530. The resulting snap shot is attached.

您需要确保您的图像看起来不像在屏幕边缘出屏幕检查x是否在screenwidth - bitmapwidth和y小于screenheight - bitmapheight 。我没有在代码中包含这些条件。

You need to make sure your image does not look like going out of screen at the screen edges Check if x is within screenwidth - bitmapwidth and y is less than screenheight - bitmapheight. i have not included those conditions in the code.

编辑:

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

这篇关于在画布上移动位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆