我想在触摸时画线,屏幕应该是透明的。
public class Draw extends Activity implements OnTouchListener
{
DrawPanel drawPanel;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
private Paint mPaint;
Path path;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_draw);
drawPanel = new DrawPanel(this);
drawPanel.setOnTouchListener(this);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(10);
FrameLayout framelayout = new FrameLayout(this);
framelayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
framelayout.addView(drawPanel);
setContentView(framelayout);
}
@Override
protected void onPause()
{
super.onPause();
drawPanel.pause();
}
@Override
protected void onResume()
{
super.onResume();
drawPanel.resume();
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
synchronized(pointsToDraw)
{
if(arg1.getAction() == MotionEvent.ACTION_DOWN)
{
path = new Path();
path.moveTo(arg1.getX(), arg1.getY());
pointsToDraw.add(path);
path.lineTo(arg1.getX()+1, arg1.getY()+1);
}
else if(arg1.getAction() == MotionEvent.ACTION_MOVE)
{
path.lineTo(arg1.getX(), arg1.getY());
}
else if(arg1.getAction() == MotionEvent.ACTION_UP)
{
//path.lineTo(arg1.getX()+2, arg1.getY()+2);
}
}
return true;
}
public class DrawPanel extends SurfaceView implements Runnable
{
Thread thread = null;
SurfaceHolder holder;
boolean isItOk = false ;
public DrawPanel(Context context)
{
super(context);
holder = getHolder();
holder.setFormat(PixelFormat.TRANSPARENT);
}
public void run()
{
while( isItOk == true){
if(!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(0, 0, 0, 0);
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
synchronized(pointsToDraw)
{
for (Path path : pointsToDraw)
{
canvas.drawPath(path, mPaint);
}
}
}
public void pause()
{
isItOk = false;
while(true)
{
try
{
thread.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
break;
}
thread = null;
}
public void resume()
{
isItOk = true;
thread = new Thread(this);
thread.start();
}
}
}
使用此代码我得到了预期的结果。但现在问题是当我使用xml放置一个按钮时,我看不到按钮。
我想在按下按钮时清除屏幕。所以请在主题半透明时帮我看按钮。
答案 0 :(得分:1)
我认为它不可能因为LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)
。您可以尝试LayoutParams(LayoutParams.FILL_PARENT,300)
。或者您可以添加 menuitem ,而不是按钮。