如果我有一个带有画布的视图对象绘制线条如何将图像转换为位图然后将其另存为PNG?我想在saveCanvasImage()方法中这样做。在下面的代码中,我假设我必须以某种方式使用getDrawingCache与tv对象。我根本没有得到关于此的内容。 (假设视图对象存在,即使在按下命令按钮之前不会发生这种情况。)
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
@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 presses on the action bar items
switch (item.getItemId()) {
case R.id.action_save:
saveCanvasImage();
return true;
case R.id.action_sign:
openTEV();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void saveCanvasImage() {
Log.d("key1","save test");
}
public void openTEV() {
Log.d("key2","set content view");
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
}
}
MyTouchEventView.java:
public class MyTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public Button btnReset;
public LayoutParams params;
public MyTouchEventView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(10f);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// resets the screen
path.reset();
// Calls the onDraw() method
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Gives you x and y coordinates on the Event.
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
case MotionEvent.ACTION_UP:
//;
break;
default:
return false;
}
// Schedules a repaint.
// Force a view to draw.
postInvalidate();
return true;
}
}
答案 0 :(得分:1)
每个View都有一个位图,其中缓存了视图图。在android参考文档中阅读View.getDrawingCache()。
注意:您还需要设置View.setDrawingCacheEnabled(true)
要回答您的评论,请在方法MyTouchEventView
中在本地创建名为tv
的{{1}}。
要使openTEV()
在其他方法中可用,您可以改为使其成为类级别对象。将它声明在MainActivity类的顶部。
tv
然后您可以将public class MainActivity extends Activity {
// Class Variables
private MyTouchEventView tv;
...
更改为:
openTEV()
然后您也可以在MainActivity类的其他位置访问它。例如:
public void openTEV() {
Log.d("key2","set content view");
tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
}