画像ビューの座標を取得するコードがあり、ユーザーが画像ビューで2回タッチしたときに線を引きたいです。imageviewには、ドローアブルからのビットマップがあります。詳細については、これが私のコードです:
public class MainActivity extends Activity implements OnTouchListener {
ImageView tampil;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
private static final String TAG = "Touch";
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
float x1, y1, x2, y2;
Bitmap bmp;
int i = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tampil = (ImageView)findViewById(R.id.imageView1);
tampil.setOnTouchListener(this);
Drawable d = getResources().getDrawable(R.drawable.enigma);
bmp = ((BitmapDrawable)d).getBitmap();
tampil.setImageBitmap(bmp);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//ImageView view = (ImageView) v;
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
Log.i(TAG, "("+String.valueOf((int)event.getX())+","+String.valueOf((int)event.getY())+")");
if (i==1){
x1 = event.getX();
y1 = event.getY();
i = 2;
Log.i(TAG, "coordinate x1 : "+String.valueOf(x1)+" y1 : "+String.valueOf(y1));
} else if (i==2){
x2 = event.getX();
y2 = event.getY();
i = 3;
Log.i(TAG, "coordinate x2 : "+String.valueOf(x2)+" y2 : "+String.valueOf(y2));
onDraw();
}
break;
}
return true;
}
public void onDraw(){
Bitmap tes = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
tes = bmp;
Canvas cvs = new Canvas(tes);
Paint pnt = new Paint();
pnt.setColor(Color.RED);
Log.i(TAG, "draw line");
cvs.drawLine(x1, y1, x2, y2, pnt);
i=1;
tampil.setImageBitmap(tes);
}
}
しかし、ユーザーが2回タッチしても、アプリケーションは線を引きません。誰でも解決策を与えることができます
これはログ猫が言うです:
10-17 12:08:21.082: I/ActivityManager(70): Displayed com.example.tesontouch/.MainActivity: +3s139ms
10-17 12:08:24.103: D/Touch(796): mode=DRAG
10-17 12:08:24.103: I/Touch(796): (113,145)
10-17 12:08:24.123: I/Touch(796): coordinate x1 : 113.26723 y1 : 145.2766
10-17 12:08:26.243: D/dalvikvm(264): GC_EXPLICIT freed 8K, 55% free 2595K/5703K, external 1625K/2137K, paused 88ms
10-17 12:08:26.323: D/Touch(796): mode=DRAG
10-17 12:08:26.323: I/Touch(796): (306,284)
10-17 12:08:26.343: I/Touch(796): coordinate x2 : 306.67017 y2 : 284.45056
10-17 12:08:26.423: D/dalvikvm(796): GC_EXTERNAL_ALLOC freed 4K, 53% free 2559K/5379K, external 3064K/4342K, paused 81ms
10-17 12:08:26.473: I/Touch(796): draw line
10-17 12:08:31.413: D/dalvikvm(473): GC_EXPLICIT freed 6K, 52% free 2697K/5511K, external 3502K/4752K, paused 183ms