package com.example.demotouch;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class MainActivity extends ActionBarActivity {
private LinearLayout ll;
private myview mv;
private Button btnBlack;
private Button btnBlue;
private Button btnred;
private Button btnclear;
private SeekBar sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll=(LinearLayout) findViewById(R.id.LinearLayout1);
btnclear=new Button(this);
btnclear.setText("Clear");
ll.addView(btnclear);
btnclear.setOnClickListener(l);
sb = new SeekBar(this);
sb.setMax(50);
ll.addView(sb);
//black
btnBlack=new Button(this);
btnBlack.setText("black");
ll.addView(btnBlack);
//blue
btnBlue=new Button(this);
btnBlue.setText("blue");
ll.addView(btnBlue);
//red
btnred=new Button(this);
btnred.setText("red");
ll.addView(btnred);
mv=new myview(this);
ll.addView(mv);
btnBlack.setOnClickListener(l);
btnBlue.setOnClickListener(l);
btnred.setOnClickListener(l);
sb.setOnSeekBarChangeListener(sk);
}
private SeekBar.OnSeekBarChangeListener sk =new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
mv.w=progress;
mv.invalidate();
}
};
private Button.OnClickListener l=new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((Button)v).getText().toString().equalsIgnoreCase("black")) mv.color=Color.BLACK;
else if(((Button)v).getText().toString().equalsIgnoreCase("blue")) mv.color=Color.BLUE;
else if(((Button)v).getText().toString().equalsIgnoreCase("red")) mv.color=Color.RED;
mv.invalidate();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
========================================================================
myview.java
package com.example.demotouch;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
public class myview extends View {
int color = Color.BLUE;
int w = 10;
private Path path = new Path();
private Paint paint = new Paint();
private PointF p;
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
paint.setColor(color);
paint.setStyle(paint.getStyle().STROKE);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
PointF p=new PointF();
p.x=event.getX();
p.y=event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
path.moveTo(p.x, p.y);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(p.x, p.y);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(color);
paint.setStrokeWidth(w);
canvas.drawPath(path, paint);
super.onDraw(canvas);
}
}
========================================================================
沒有留言:
張貼留言