我有一个ImageView的,帆布,和一个按钮.. 当我点击按钮的位图被画在画布上
我想用我的onTouch(位图拖动到任何地方在画布上)移动的位图。
s.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>父,
视图V,INT位置,长的id){
位图workingBitmap = Bitmap.createBitmap(currentBitmap);
workingBitmap = Bitmap.createBitmap(workingBitmap);
帆布C =新的Canvas(workingBitmap);
brightBitmap = BitmapFactory.de codeResource(getResources(),sIds.mSmileyIds [位置],NULL);
brightBitmap = Bitmap.createScaledBitmap(brightBitmap,100,100,FALSE);
chosenSmiley = brightBitmap;
如果(chosenSmiley!= NULL){
尝试 {
c.drawBitmap(chosenSmiley,POSX,波西,NULL);
}赶上(例外五){
e.printStackTrace();
}
}
iv.setImageBitmap(workingBitmap);
}
公共布尔的onTouchEvent(MotionEvent事件){
INT eventaction = event.getAction();
开关(eventaction){
案例MotionEvent.ACTION_DOWN:
//手指触摸屏幕
打破;
案例MotionEvent.ACTION_MOVE:
//手指在屏幕上移动
lastX =(int)的event.getX();
lastY =(int)的event.getY();
Log.e(我的XName,lastX +lastX的COORDS);
Log.e(我的XName,lastY +COORDS的lastY);
brightBitmap = Bitmap.createScaledBitmap(brightBitmap,lastX,lastY,假);
打破;
案例MotionEvent.ACTION_UP:
//手指离开屏幕
打破;
}
//告诉我们处理的事件,也没有进一步的处理所需的系统
返回true;
}
});
这是我目前的code,位图得到的0,0创建的,但我不能将它拖到等。
解决方案 公共类MainActivity扩展活动
{
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
DrawingView DV =新DrawingView(本);
的setContentView(DV);
}
类DrawingView扩展视图{
点阵位图;
浮动的x,y;
公共DrawingView(上下文的背景下)
{
超(上下文);
位= BitmapFactory.de codeResource(context.getResources(),R.drawable.ic_launcher);
}
公共布尔的onTouchEvent(MotionEvent事件)
{
开关(event.getAction())
{
案例MotionEvent.ACTION_DOWN:{
}
打破;
案例MotionEvent.ACTION_MOVE:
{
X =(int)的event.getX();
Y =(INT)event.getY();
无效();
}
打破;
案例MotionEvent.ACTION_UP:
X =(int)的event.getX();
Y =(INT)event.getY();
的System.out.println(..................+ X +......+ Y); // X = 345 Y = 530
无效();
打破;
}
返回true;
}
@覆盖
公共无效的OnDraw(帆布油画)
{
涂料粉刷=新的油漆();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.CYAN);
canvas.drawBitmap(位图,X,Y,油漆); //在x = O和y = 0最初的位图绘制
}
}
}
目前星位图绘制在x = 0和y = 0;在拖动X和Y的变化这么叫inavlidate刷新水合物。在触摸了绘制位图在拖动位置x和y,并呼吁无效刷新平局。
位图在x = 0 y = 0的绘制。在拖放// X = 345 Y = 530。由此产生的快照附后。
您需要确保你的形象并不像走出去的屏幕在屏幕边缘检查x是在屏幕宽度 - bitmapwidth和y小于screenheight - bitmapheight。我还没有包括在code的条件。
编辑:
的setContentView(R.layout.main);
的LinearLayout LL =(的LinearLayout)findViewById(R.id.ll);
DrawingView DV =新DrawingView(本);
ll.addView(DV);
I have an imageView, canvas, and a button.. when I click the button a bitmap gets drawn on the canvas
I want to move that bitmap using my onTouch ( drag the bitmap to anywhere 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;
}
});
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
}
}
}
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.
Bitmap drawn at x=0 y=0. on drag and drop //x= 345 y=530. The resulting snap shot is attached.
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.
EDIT:
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
DrawingView dv= new DrawingView(this);
ll.addView(dv);