用户如何在触摸时也能画出线条跟随手指?Android 开发
Posted
技术标签:
【中文标题】用户如何在触摸时也能画出线条跟随手指?Android 开发【英文标题】:How can the user also draw on touch so the line follow his finger?Android development 【发布时间】:2013-04-03 16:18:19 【问题描述】:我有这个代码来为教练制作一个篮球板,用户可以在其中移动球员并改变背景球场。 现在用户可以画出跟随他手指的线条。我怎样才能做到这一点? 我搜索它,我发现我必须创建一个 View 类,但我无法连接。也许有人我要写什么代码?
public class HalfCourt extends Activity
/** Called when the activity is first created. */
ImageView img, img2, img3, img4, img5, img6, img7, img8, img9, img10 =null;
AbsoluteLayout aLayout;
public LayoutParams params, params2,params3,params4, params5, params6, parmas7, params8, params9, params10;
int status=0;
public Item red_out;
public Button lock,reset, erase;
MyTouchListener touchlistener;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.halfcourt);
//View custom = (View)findViewById(R.id.view1);
aLayout= (AbsoluteLayout)findViewById(R.id.absLayout);
img=(ImageView)findViewById(R.id.imageView);
img2=(ImageView)findViewById(R.id.imageView22);
img3=(ImageView)findViewById(R.id.imageView3);
img4=(ImageView)findViewById(R.id.imageView4);
img5=(ImageView)findViewById(R.id.imageView5);
img6=(ImageView)findViewById(R.id.imageView6);
img7=(ImageView)findViewById(R.id.imageView7);
img8=(ImageView)findViewById(R.id.imageView8);
img9=(ImageView)findViewById(R.id.imageView9);
img10=(ImageView)findViewById(R.id.imageView10);
lock = (Button)findViewById(R.id.lock_button);
reset = (Button)findViewById(R.id.reset_button);
erase = (Button)findViewById(R.id.erase_button);
/*
*
* Αρχικοποίηση των TouchListeners
*
* */
img.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=1;
return false;
);
img2.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=2;
return false;
);
img3.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=3;
return false;
);
img4.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=4;
return false;
);
img5.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=5;
return false;
);
img6.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=6;
return false;
);
img7.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=7;
return false;
);
img8.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=8;
return false;
);
img9.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=9;
return false;
);
img10.setOnTouchListener(new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
status=10;
return false;
);
touchlistener = new MyTouchListener();
aLayout.setOnTouchListener(touchlistener);
LockXandO locking = new LockXandO();
lock.setOnClickListener(locking);
lock.setText("Lock");
ResetPositions reseting = new ResetPositions();
reset.setOnClickListener(reseting);
public boolean onCreateOptionsMenu(Menu menu)
super.onCreateOptionsMenu(menu);
MenuInflater mitsos = getMenuInflater();
mitsos.inflate(R.menu.half_court_menu, menu);
return true;
public boolean onOptionsItemSelected(MenuItem item)
switch(item.getItemId())
case R.id.red_out:
if (item.getTitle()== "Αφαίρεσε τους κόκκινους")
item.setTitle("Πρόσθεσε τους κόκκινους");
else
item.setTitle("Αφαίρεσε τους κόκκινους");
return true;
case R.id.change_to_full_court:
startActivity(new Intent("com.mitsos.thebasic.TUTORIAL3"));
return true;
return false;
public class LockXandO implements OnClickListener
/**
* Η μέθοδος όπου κλειδώνω και ξεκλειδώνω τους "Παίκτες"
*/
@Override
public void onClick(View v)
/**
* Εδώ διαλέγουμε αν είμαστε σε κατάσταση κλειδώματος ή ξεκλειδώματος
**/
if (lock.getText() == "Lock")
kleidwse();
lock.setText("Unlock");
else
ksekleidwse();
lock.setText("Lock");
private void kleidwse()
/*
* Με αυτή τη μέθοδο κλειδώνουν οι "Παίκτες"
*/
aLayout.setOnTouchListener(null);
public void ksekleidwse()
/**
* Με αυτή τη μέθοδο ξεκλειδώνουν οι "Παίκτες"
**/
aLayout.setOnTouchListener(touchlistener);
public class ResetPositions implements OnClickListener
@Override
public void onClick(View v)
ResetMe();
ksekleidwse();
lock.setText("Lock");
private void ksekleidwse()
/**
* Την ξαναχρησιμοποιώ εδώ για να ξεκλειδώσουν οι παίχτες καθώς επανέρχονται στις αρχικές θέσεις
**/
aLayout.setOnTouchListener(touchlistener);
@SuppressWarnings("deprecation")
private void ResetMe()
aLayout.setOnTouchListener(touchlistener);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,105,250);
img.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,165,250);
img2.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,225,250);
img3.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,285,250);
img4.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,345,250);
img5.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,105,330);
img6.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,165,330);
img7.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,225,330);
img8.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,285,330);
img9.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,345,330);
img10.setLayoutParams(params);
aLayout.setOnTouchListener(touchlistener);
public class MyTouchListener implements OnTouchListener
@SuppressWarnings("deprecation")
@Override
public boolean onTouch(View v, MotionEvent event)
/*
*
* Επιλογή της εικόνας που θα κινηθεί με το status
*
*/
if(status==1)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
img.setLayoutParams(lp);
else if (status ==2)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img2.getWidth()/2,(int)event.getY()-img2.getHeight()/2);
img2.setLayoutParams(lp);
else if (status ==3)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img3.getWidth()/2,(int)event.getY()-img3.getHeight()/2);
img3.setLayoutParams(lp);
else if (status ==4)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img4.getWidth()/2,(int)event.getY()-img4.getHeight()/2);
img4.setLayoutParams(lp);
else if (status ==5)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img2.getWidth()/2,(int)event.getY()-img2.getHeight()/2);
img5.setLayoutParams(lp);
else if (status ==6)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img6.getWidth()/2,(int)event.getY()-img6.getHeight()/2);
img6.setLayoutParams(lp);
else if (status ==7)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img7.getWidth()/2,(int)event.getY()-img7.getHeight()/2);
img7.setLayoutParams(lp);
else if (status ==8)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img8.getWidth()/2,(int)event.getY()-img8.getHeight()/2);
img8.setLayoutParams(lp);
else if (status ==9)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img9.getWidth()/2,(int)event.getY()-img9.getHeight()/2);
img9.setLayoutParams(lp);
else if (status ==10)
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
(int)event.getX()-img10.getWidth()/2,(int)event.getY()-img10.getHeight()/2);
img10.setLayoutParams(lp);
if(event.getAction()==MotionEvent.ACTION_UP)
status=0;
return true;
public class FingerDrawingView extends View
private static final boolean DEBUG = false;
private static final float MAXP = 0.75F;
private static final float MINP = 0.25F;
private static final String TAG = "FingerDrawingView";
private static final float TOUCH_TOLERANCE = 4.0F;
private Bitmap mBackupBitmap;
private Bitmap mBitmap;
private Paint mBitmapPaint;
private Canvas mCanvas;
private boolean mDrawingEnable = false;
private MaskFilter mEmboss;
private int mHeight;
private Paint mPaint = new Paint();
private Path mPath;
private int mWidth;
private float mX;
private float mY;
public FingerDrawingView(Context paramContext)
super(paramContext);
this.mPaint.setAntiAlias(true);
this.mPaint.setDither(true);
this.mPaint.setColor(-1);
this.mPaint.setStyle(Paint.Style.STROKE);
this.mPaint.setStrokeJoin(Paint.Join.ROUND);
this.mPaint.setStrokeCap(Paint.Cap.ROUND);
this.mPaint.setStrokeWidth(5.0F);
private void touch_move(float paramFloat1, float paramFloat2)
float f1 = Math.abs(paramFloat1 - this.mX);
float f2 = Math.abs(paramFloat2 - this.mY);
if ((f1 >= 4.0F) || (f2 >= 4.0F))
this.mPath.quadTo(this.mX, this.mY, (paramFloat1 + this.mX) / 2.0F, (paramFloat2 + this.mY) / 2.0F);
this.mX = paramFloat1;
this.mY = paramFloat2;
private void touch_start(float paramFloat1, float paramFloat2)
this.mPath.reset();
this.mPath.moveTo(paramFloat1, paramFloat2);
this.mX = paramFloat1;
this.mY = paramFloat2;
private void touch_up()
this.mPath.lineTo(this.mX, this.mY);
this.mCanvas.drawPath(this.mPath, this.mPaint);
this.mPath.reset();
public void backupCanvas()
this.mBackupBitmap = Bitmap.createBitmap(this.mBitmap);
public boolean getDrawingEnable()
return this.mDrawingEnable;
public void initCanvas(int paramInt1, int paramInt2)
this.mWidth = paramInt1;
this.mHeight = paramInt2;
this.mBitmap = Bitmap.createBitmap(paramInt1, paramInt2, Bitmap.Config.ARGB_8888);
this.mCanvas = new Canvas(this.mBitmap);
this.mPath = new Path();
this.mBitmapPaint = new Paint(4);
this.mBitmapPaint.setAlpha(16777215);
public boolean loadCanvas(String paramString)
Bitmap localBitmap = BitmapFactory.decodeFile(paramString);
if (localBitmap != null)
this.mBitmap = localBitmap.copy(Bitmap.Config.ARGB_8888, true);
this.mCanvas.setBitmap(this.mBitmap);
invalidate();
return true;
return false;
protected void onDraw(Canvas paramCanvas)
if ((this.mBitmap != null) && (this.mPath != null))
paramCanvas.drawBitmap(this.mBitmap, 0.0F, 0.0F, this.mBitmapPaint);
paramCanvas.drawPath(this.mPath, this.mPaint);
protected void onSizeChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
super.onSizeChanged(paramInt1, paramInt2, paramInt3, paramInt4);
【问题讨论】:
请格式化您的代码。 【参考方案1】:Donn Felker 有一些示例代码(取自 Square),可以在图像上平滑绘制。见the sample code in Donn's repository
【讨论】:
对不起我的烂摊子!我想做这样的白色线条:play.google.com/store/apps/… 我该怎么做?以上是关于用户如何在触摸时也能画出线条跟随手指?Android 开发的主要内容,如果未能解决你的问题,请参考以下文章