タッチで線を引きたいのですが、画面は透明にする必要があります。
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を使用してボタンを配置したときにボタンが表示されないことです。
ボタンが押されたときに画面をクリアしたい。テーマが半透明のときにボタンが見えるようにしてください。