研究了好久 都写不好 。。。
package com.li.zhuoyuan.designmodel;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
public abstract class GestureDetector {
private static final String TAG = "VersionedGestureDetector";
OnGestureListener mListener;
public static GestureDetector newInstance(Context context,
OnGestureListener listener) {
final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
GestureDetector detector = null;
if (sdkVersion < Build.VERSION_CODES.ECLAIR) {
detector = new CupcakeDetector();
} else if (sdkVersion < Build.VERSION_CODES.FROYO) {
detector = new EclairDetector();
} else {
detector = new FroyoDetector(context);
}
Log.d(TAG, "Created new " + detector.getClass());
detector.mListener = listener;
return detector;
}
public abstract boolean onTouchEvent(MotionEvent ev);
public interface OnGestureListener {
public void onDrag(float dx, float dy);
public void onScale(float scaleFactor);
}
private static class CupcakeDetector extends GestureDetector {
float mLastTouchX;
float mLastTouchY;
float getActiveX(MotionEvent ev) {
return ev.getX();
}
float getActiveY(MotionEvent ev) {
return ev.getY();
}
boolean shouldDrag() {
return true;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
mLastTouchX = getActiveX(ev);
mLastTouchY = getActiveY(ev);
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = getActiveX(ev);
final float y = getActiveY(ev);
if (shouldDrag()) {
mListener.onDrag(x - mLastTouchX, y - mLastTouchY);
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
}
return true;
}
}
private static class EclairDetector extends CupcakeDetector {
private static final int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;
private int mActivePointerIndex = 0;
@Override
float getActiveX(MotionEvent ev) {
return ev.getX(mActivePointerIndex);
}
@Override
float getActiveY(MotionEvent ev) {
return ev.getY(mActivePointerIndex);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
//No.1
//开始写代码,请结合所有代码在这里实现,计算手指在屏幕上移动的位置。从而方便其他调用这个类的地方更新屏幕上小机器人的位置。小机器人跟随手指移动。
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mLastTouchX = ev.getX();
mLastTouchY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
mLastTouchX = ev.getX();
mLastTouchY = ev.getY();
}
mActivePointerIndex = 0;
//end_code
return super.onTouchEvent(ev);
}
}
private static class FroyoDetector extends EclairDetector {
private ScaleGestureDetector mDetector;
public FroyoDetector(Context context) {
mDetector = new ScaleGestureDetector(context,
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mListener.onScale(detector.getScaleFactor());
return true;
}
});
}
@Override
boolean shouldDrag() {
return !mDetector.isInProgress();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
mDetector.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
}
}
package com.li.zhuoyuan.designmodel;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private Button button_save, button_resume;
private ImageView iv_canvas;
private Bitmap baseBitmap;
private Canvas canvas;
/*声明画笔*/
private Paint mPaint;
/*手势监听器*/
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
/*表示手指在屏幕上X轴Y轴的坐标点*/
float startX;
float startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//No.1
//开始写代码,请用代码实现初始化Bitmap对象、定义画布颜色(请定义为白色)、记录用户手指在屏幕上的触摸点坐标 将坐标存入到startX和startY变量中。
Bitmap bitmap = Bitmap.createBitmap(iv_canvas.getWidth(),
iv_canvas.getHeight(), Bitmap.Config.ARGB_8888);
mPaint.setColor(Color.WHITE);
startX = iv_canvas.getX();
startX = iv_canvas.getY();
//end_code
break;
case MotionEvent.ACTION_MOVE:
//No.2
//开始写代码,根据手指在屏幕滑动的轨迹实现绘制轨迹的线条,并更新手指在屏幕上的坐标点(startX和startY变量),将绘制好的图像展示在ImageView中。
float stopX = iv_canvas.getX();
float stopY = iv_canvas.getY();
//end_code
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*初始化画笔 设置画笔的宽度和颜色*/
mPaint = new Paint();
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.RED);
iv_canvas = (ImageView) findViewById(R.id.iv_canvas);
button_save = (Button) findViewById(R.id.button_save);
button_resume = (Button) findViewById(R.id.button_resume);
/*保存按钮的监听器*/
button_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveBitmap();
}
});
/*清除按钮的监听器*/
button_resume.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
resumeCanvas();
}
});
iv_canvas.setOnTouchListener(onTouchListener);
}
/*保存图片的方法*/
protected void saveBitmap() {
try {
/*以png的格式保存的手机内存中*/
//No.3
//开始写代码,请在这里实现文件存储的方法,将手指在屏幕上涂鸦的图形以PNG图片的形式保存到SD卡中;并发送广播进行扫描SD卡。
File file
//end_code
} catch (Exception e) {
Toast.makeText(MainActivity.this, "保存图片失败", 0).show();
e.printStackTrace();
}
}
/*清除画布的方法*/
protected void resumeCanvas() {
if (baseBitmap != null) {
baseBitmap = Bitmap.createBitmap(iv_canvas.getWidth(),
iv_canvas.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(baseBitmap);
canvas.drawColor(Color.WHITE);
iv_canvas.setImageBitmap(baseBitmap);
Toast.makeText(MainActivity.this, "清除画板成功,可以重新开始绘图", 0).show();
}
}
}
请输入代码
Dites-moi simplement quelle fonction vous souhaitez créer, afin que personne ne revienne en arrière et vous aide à remplir le code
Pouvez-vous partager votre salaire avec moi ?