我在onTouchEvent
中传递DravingView
的X和Y坐标时出现问题;我想在MainActivity
中显示这些值。我尝试过使用intinity和bundle方法,但是我做错了。
DrawingView.java
public class DrawingView extends View {
private Bitmap cacheBitmap;
private Canvas cacheCanvas;
private Paint paint;
private Paint BitmapPaint;
private Path path;
private int height;
private int width;
/** Last saved X-coordinate */
private float pX;
/** Last saved Y-coordinate*/
private float pY;
/** Initial color */
private int paintColor = Color.BLACK;
private static Paint.Style paintStyle = Paint.Style.STROKE;
/** Paint Point size */
private static int paintWidth = 8;
private Canvas canvas;
/** get the height and width */
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
height = h;
width = w;
init();
}
private void init(){
cacheBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
cacheCanvas = new Canvas(cacheBitmap);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
path = new Path();
BitmapPaint = new Paint();
updatePaint();
}
private void updatePaint(){
paint.setColor(paintColor);
paint.setStyle(paintStyle);
paint.setStrokeWidth(paintWidth);
}
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawingView(Context context){
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
pX = event.getX();
pY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
path.quadTo(pX, pY, event.getX(), event.getY());
pX = event.getX();
pY = event.getY();
break;
case MotionEvent.ACTION_UP:
cacheCanvas.drawPath(path, paint);
path.reset();
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
BitmapPaint = new Paint();
canvas.drawBitmap(cacheBitmap, 0,0, BitmapPaint);
canvas.drawPath(path, paint);
}
public void setColor(int color){
paintColor = color;
updatePaint();
}
public void setPaintWidth(int width){
paintWidth = width;
updatePaint();
}
public static final int PEN = 1;
public static final int PAIL = 2;
public void setStyle(int style){
switch(style){
case PEN:
paintStyle = Paint.Style.STROKE;
break;
case PAIL:
paintStyle = Paint.Style.FILL;
break;
}
updatePaint();
}
/** clear your drawing*/
public void clearScreen(){
if(canvas != null){
Paint backPaint = new Paint();
backPaint.setColor(Color.WHITE);
canvas.drawRect(new Rect(0, 0, width, height), backPaint);
cacheCanvas.drawRect(new Rect(0, 0, width, height), backPaint);
}
invalidate();
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private DrawingView drawView;
private ImageButton currPaint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = (DrawingView)findViewById(R.id.drawing);
//LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void openMenuBack(View view) {
Intent intent = new Intent(this, MainActivityMenu.class);
startActivity(intent);
}
public void openNew(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
答案 0 :(得分:0)
你可以做的是创建一个你的mainActivity将实现的接口。
public interface MyDrawingViewInterface {
public void onActionFinished(int movedX, int movedY);
}
然后您的MainActivity将实现此接口:
public class MainActivity extends ActionBarActivity implements MyDrawingViewInterface{
/* All your other stuff */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = (DrawingView)findViewById(R.id.drawing);
drawView.setViewListener(this);
//LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
}
@Override
public void onActionFinished(int movedX, int movedY) {
/*Print it wherever you want here */
}
}
最后,您的DrawView将如此改变(注意新变量,以及对ACTION_UP的调用):
public class DrawingView extends View {
private MyDrawingInterface mInterface;
public void setViewListener(MyDrawingView interface) {
mInterface = interface;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
pX = event.getX();
pY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
path.quadTo(pX, pY, event.getX(), event.getY());
pX = event.getX();
pY = event.getY();
break;
case MotionEvent.ACTION_UP:
cacheCanvas.drawPath(path, paint);
path.reset();
mInterface.onActionFinished(pX, pY);
break;
}
invalidate();
return true;
}
}