[英]How to drawing on surface view canvas in android
從表面上看,我有一個問題。 實際上,當我在表面視圖上繪制任何對象時,在drawing(onMoving)期間不會顯示,但是當我從表面視圖上松開手指時,可以看到繪制對象。 因此,任何人都可以解決它。
我的代碼是......
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
currentDrawingPath = new DrawingPath();
currentDrawingPath.paint = currentPaint;
currentDrawingPath.path = new Path();
currentBrush.mouseDown(currentDrawingPath.path, motionEvent.getX(), motionEvent.getY());
}else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
currentBrush.mouseMove( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );
}else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
currentBrush.mouseUp( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );
drawingSurface.addDrawingPath(currentDrawingPath);
}
return true;
}
試試這個代碼
Ourview v;
Bitmap ball;
float x,y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new Ourview(this);
v.setOnTouchListener(this);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.circle_green);
x = y = 0;
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class Ourview extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false;
public Ourview(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void run() {
// TODO Auto-generated method stub
while (isItOk == true){
//perfom convas drawing
if (!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(250, 10, 50, 100);
c.drawBitmap(ball, x - (ball.getWidth()/2), y - (ball.getHeight()/2), null);
holder.unlockCanvasAndPost(c);
}
}
public void pause(){
isItOk = false;
while(true){
try{
t.join();
}catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
}
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_UP:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_MOVE:
x = me.getX();
//y = me.getY();
break;
}
return true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.