我正在开发一个android绘图应用程序。当我运行这个代码时,它说应用程序在logcat的主线程上做了太多的工作。我不能绘图。请帮助我。谢谢
这是我的绘画课
public class Drawit extends Activity {
LinearLayout linearLayout;
ImageButton oclear,save,ocancel;
View mview;
DrawCanvas drawCanvas;
public static String tempDir;
public static String unique_id;
public static String current=null;
public Bitmap mBitmap;
public EditText yourName;
File myPath;
protected void onCreate(Bundle savedInsatnceState) {
// TODO Auto-generated method stub
super.onCreate(savedInsatnceState);
setContentView(R.layout.drawit);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout11);
tempDir = Environment.getExternalStorageDirectory()+"/"+getResources().getString(R.string.external_dir);
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir(getResources().getString(R.string.external_dir),MODE_PRIVATE);
preparedirectory();
yourName = (EditText) findViewById(R.id.editText1);
myPath = new File(directory,current);
drawCanvas = new DrawCanvas(this, null);
drawCanvas.setBackgroundColor(Color.WHITE);
linearLayout.addView(drawCanvas,LayoutParams.FILL_PARENT);
}
}
这是我画画布的方法
public class DrawCanvas extends View
{
public Paint paint = new Paint();
public Path path = new Path();
public final RectF dirtyRect = new RectF();
public float lastTouchX;
public float lastTouchY;
public DrawCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5f);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setAntiAlias(true);
}
protected void ondraw(Canvas canvas) {
canvas.drawPath(path,paint);
}
public void save(View mview) throws IOException{
// TODO Auto-generated method stub
if(mBitmap == null)
{
mBitmap = Bitmap.createBitmap(linearLayout.getWidth(),linearLayout.getHeight(),Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(mBitmap);
try {
FileOutputStream fileoutputstream = new FileOutputStream(myPath);
mview.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.PNG , 90, fileoutputstream);
fileoutputstream.flush();
fileoutputstream.close();
String uri = Images.Media.insertImage(getContentResolver(),mBitmap,"title",null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void clear() {
// TODO Auto-generated method stub
path.reset();
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
resetDirtyRect(eventX,eventY);
int historyRectSize = event.getHistorySize();
for(int i=0;i<historyRectSize;i++)
{
float historicalx = event.getHistoricalX(i);
float historicaly = event.getHistoricalY(i);
expandDirtyRect(historicalx,historicaly);
path.lineTo(historicalx, historicaly);
}
break;
}
return true;
}
private void expandDirtyRect(float historicalx, float historicaly) {
// TODO Auto-generated method stub
if(historicalx < dirtyRect.left)
{
dirtyRect.left = historicalx;
} else if(historicalx >dirtyRect.right)
{
dirtyRect.left = historicalx;
}
if(historicaly < dirtyRect.top)
{
dirtyRect.top = historicaly;
} else if(historicaly >dirtyRect.bottom)
{
dirtyRect.bottom = historicaly;
}
}
private void resetDirtyRect(float eventX, float eventY) {
// TODO Auto-generated method stub
dirtyRect.left = Math.min(lastTouchX,eventX);
dirtyRect.right = Math.max(lastTouchX,eventX);
dirtyRect.top = Math.min(lastTouchY,eventY);
dirtyRect.bottom = Math.max(lastTouchY,eventY);
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!