gpt4 book ai didi

android - 在 Canvas 上移动位图

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:49 24 4
gpt4 key购买 nike

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

我想使用我的 onTouch 移动该位图(将位图拖到 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 上创建的,但我不能拖动它等等 ..

最佳答案

 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 刷新绘制。

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

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

enter image description here

编辑:

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

关于android - 在 Canvas 上移动位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com