在画布上移动位图

6
我有一个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处创建,但我无法拖动它等等...

您有一个按钮,当点击它时会绘制图像。然后可以用手指移动该图像。这是您要找的吗? - Raghunandan
1个回答

8
 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以刷新绘制。在触摸抬起时,在拖动的位置x和y处绘制位图,并调用invalidate以刷新绘制。
位图绘制在x=0 y=0处。在拖放时//x= 345 y=530。附上结果快照。
您需要确保图像不会超出屏幕边缘,请检查x是否在screenwidth-bitmapwidth范围内,y是否小于screenheight-bitmapheight。我没有在代码中包含这些条件。
编辑:
      setContentView(R.layout.main);    
      LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
      DrawingView dv= new DrawingView(this);
      ll.addView(dv);

您可以设置视图的大小。上述代码获取屏幕的高度和宽度。我可以拥有其他UI元素并最小化画布绘制空间。 - Raghunandan
你可以使用setContentView(R.layout.main)来设置你的布局。现在在main.xml文件中定义一个布局,比如线性布局,设置它的宽度和高度,并将绘图视图添加到你的布局中。 - Raghunandan
您可以使用手指拖动,释放后位图将绘制到该位置。在单击而不是图像视图上绘制图像到画布上。 - Raghunandan
检查编辑。main.xml 可以有按钮。设置您的布局。单击时调用 invalidate,以便调用 Drawingaviews 绘制。在 onDraw 中绘制图像。 - Raghunandan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接