我的活動課文件:繪畫通過觸摸屏跳過(不連續線)
package com.drawing.test;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
public class TstActivity extends Activity implements OnTouchListener
{
float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
public static boolean action=false;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mBitmapPaint;
Drawer mDrawer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.drawView);
mLinearLayout.setOnTouchListener((OnTouchListener) this);
mLinearLayout.addView(new Drawer(this));
}
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
action=false;
//v.invalidate();
return true;
case MotionEvent.ACTION_MOVE:
x1=x2;
y1=y2;
x2 = event.getX();
y2 = event.getY();
v.invalidate();
action=true;
return true;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
v.invalidate();
action=true;
return true;
}
return false;
}
public class Drawer extends View
{
public Drawer(Context context)
{
super(context);
mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmapPaint.setColor(Color.MAGENTA);
invalidate();
}
protected void onDraw(Canvas canvas)
{
if(x1==x2 && y1==y2)return;
Paint p = new Paint();
// Canvas mCanvas1=new Canvas(mBitmap);
p.setColor(Color.parseColor("#7CFC00"));
canvas.drawBitmap(mBitmap, 0, 0, p);
// canvas.drawLine(x1, y1, x2 , y2, p);
p.setColor(Color.RED);
// mCanvas1.drawLine(x1, y1, x2, y2,p);
if(action==true)mCanvas.drawLine(x1, y1, x2, y2, mBitmapPaint);
}
}
}
我的佈局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/drawView">
</LinearLayout>
當我嘗試這一點上模擬器,換行符在中間,但繼續繪製。我也試過設備,同樣的問題發生但數量較少。此外,顯示器有時閃爍,或完全空白。當它完全黑色時,如果我觸摸一次就會回來。
我在做什麼錯,我應該改變我的方法。
任何建議表示讚賞。謝謝
見我的答案,但請你提高標題:)我建議一個 – Raffaele 2012-03-26 20:30:33
謝謝...我會把一個類似的標題,但我不明白什麼是技術問題:) – kishu27 2012-03-27 05:29:55