Android 当我转动手机时,我的视野总是清晰的
我创建了一个绘画视图,当我旋转我的手机时,我的绘画总是清晰的,我不知道为什么是清晰的 这是我的全部活动:Android 当我转动手机时,我的视野总是清晰的,android,Android,我创建了一个绘画视图,当我旋转我的手机时,我的绘画总是清晰的,我不知道为什么是清晰的 这是我的全部活动: public class SignatureActivity extends Activity implements Handler.Callback { File mCurrentPhotoPath; private FrameLayout drawerBox; private Button save, clear; private DrawerView
public class SignatureActivity extends Activity implements Handler.Callback {
File mCurrentPhotoPath;
private FrameLayout drawerBox;
private Button save, clear;
private DrawerView mv;
private Paint mPaint;
private boolean existingSign;
private Intent intent;
private String signature;
private File imagesDir;
private ArrayList<MotionEvent> motionEvents = new ArrayList<>();
private ArrayList<MotionEvent> auxMotionEvents = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_act_lay);
existingSign = getIntent().hasExtra(getPackageName() + "_signaturePath");
initializeView();
setListeners();
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
signature = (String) b.get("signaturePath");
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
finish();
}
private void initializeView() {
drawerBox = this.findViewById(R.id.drawerContainer);
save = findViewById(R.id.saveBtn);
clear = findViewById(R.id.clearBtn);
if (existingSign) {
mv = new DrawerView(this, BitmapFactory.decodeFile(getIntent()
.getStringExtra(getPackageName() + "_signaturePath")));
} else {
mv = new DrawerView(this);
}
mv.setDrawingCacheEnabled(false);
mv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
drawerBox.addView(mv);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
mv.setDrawingCacheEnabled(true);
}
// @Override
// protected void onRestoreInstanceState(Bundle savedInstanceState) {
//
// mCurrentPhotoPath = (File) savedInstanceState.getSerializable(MY_CURRENT_PHOTO_PATH_KEY);
// super.onRestoreInstanceState(savedInstanceState);
// }
//
// @Override
// protected void onSaveInstanceState(Bundle outState) {
//
// outState.putSerializable(MY_CURRENT_PHOTO_PATH_KEY, mCurrentPhotoPath);
// super.onSaveInstanceState(outState);
// }
private void setListeners() {
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mv.isSigned()) {
Toast.makeText(SignatureActivity.this, R.string.empty_sign, Toast.LENGTH_SHORT).show();
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
String currentDateandTime = sdf.format(new Date());
String name = "sign_" + currentDateandTime;
Bitmap bitmap = mv.getDrawingCache();
File imagesFolder = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
imagesFolder.mkdirs();
File file = new File(imagesFolder, name + ".jpg");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.close();
mv.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
mv.setDrawingCacheEnabled(true);
}
Intent data = new Intent();
data.putExtra(getPackageName() + "_signaturePath", file.getPath());
setResult(RESULT_OK, data);
finishWithResult(file.getPath());
if (signature != null) {
File file1 = new File(signature);
file1.delete();
}
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mv.clear();
mv.setIsSigned(false);
}
});
}
private void finishWithResult(String path) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
intent.putExtra("path", path);
finish();
}
@Override
public boolean handleMessage(Message msg) {
return false;
}
public class DrawerView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private float mX, mY, sX, sY;
private boolean isSigned = false;
private static final String EXTRA_EVENT_LIST = "event_list";
private static final String EXTRA_STATE = "instance_state";
// private ArrayList<MotionEvent> eventList = new ArrayList<MotionEvent>(100);
public DrawerView(Context c) {
super(c);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmapPaint.setColor(Color.WHITE);
setSaveEnabled(true);
}
public DrawerView(Context c, Bitmap bitmap) {
super(c);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmapPaint.setColor(Color.WHITE);
mBitmap = bitmap;
setSaveEnabled(true);
}
@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(EXTRA_STATE, super.onSaveInstanceState());
bundle.putParcelableArrayList(EXTRA_EVENT_LIST, motionEvents);
return bundle;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
auxMotionEvents = new ArrayList<>();
motionEvents = new ArrayList<>();
Bundle bundle = (Bundle) state;
super.onRestoreInstanceState(bundle.getParcelable(EXTRA_STATE));
auxMotionEvents = bundle.getParcelableArrayList(EXTRA_EVENT_LIST);
if (auxMotionEvents == null) {
auxMotionEvents = new ArrayList<>();
}
return;
}
super.onRestoreInstanceState(state);
}
public void setIsSigned(boolean isSigned) {
this.isSigned = isSigned;
}
public boolean isSigned() {
return isSigned;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas = new Canvas(mBitmap);
mCanvas.drawColor(Color.WHITE);
if (motionEvents != null) {
for (MotionEvent motionEvent : motionEvents) {
performTouch(motionEvent);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
public void clear() {
setDrawingCacheEnabled(true);
mCanvas.drawColor(Color.WHITE);
invalidate();
}
private void drawDot(float x, float y) {
mCanvas.drawCircle(x, y, 2.0f, mPaint);
}
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
sX = x;
sY = y;
}
private void touch_move(float x, float y) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
mCanvas.drawPath(mPath, mPaint);
}
private void touch_up() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
private void performTouch(MotionEvent event) {
this.isSigned = true;
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
// motionEvents.add(MotionEvent.obtain(event));
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
// motionEvents.add(MotionEvent.obtain(event));
break;
case MotionEvent.ACTION_UP:
if (x == sX && y == sY) {
drawDot(x, y);
}
touch_up();
// motionEvents.add(MotionEvent.obtain(event));
break;
}
invalidate();
motionEvents.add(MotionEvent.obtain(event));
// motionEventsList.add(MotionEvent.obtain(event));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
motionEvents.add(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
performTouch(event);
}
return true;
}
}
}
公共类SignatureActivity扩展了活动实现处理程序。回调{
文件mCurrentPhotoPath;
专用框架布局抽屉盒;
私有按钮保存,清除;
私人抽屉视图mv;
私人油漆;
私有布尔存在符号;
私人意图;
私有字符串签名;
私有文件图像;
private ArrayList motionEvents=新建ArrayList();
私有ArrayList auxMotionEvents=新ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、抽屉动作);
existingSign=getIntent().hasExtra(getPackageName()+“_signaturePath”);
初始化视图();
setListeners();
Intent iin=getIntent();
Bundle b=iin.getExtras();
如果(b!=null){
签名=(字符串)b.get(“签名路径”);
}
}
@凌驾
受保护的void onResume(){
super.onResume();
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
}
@凌驾
public void onBackPressed(){
setResult(结果被取消);
完成();
}
private void initializeView(){
抽屉盒=this.findviewbyd(R.id.drawerContainer);
save=findviewbyd(R.id.saveBtn);
clear=findViewById(R.id.clearBtn);
如果(现有符号){
mv=新的抽屉视图(这是BitmapFactory.decodeFile(getIntent())
.getStringExtra(getPackageName()+“_signaturePath”);
}否则{
mv=新抽屉视图(本);
}
mv.setDrawingCacheEnabled(假);
mv.setLayoutParams(新的ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,
ViewGroup.LayoutParams.MATCH_PARENT);
抽屉盒添加视图(mv);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(油漆、样式、笔划);
mPaint.setStrokeJoin(油漆.连接.圆形);
mPaint.setStrokeCap(油漆盖圆形);
mPaint.设定行程宽度(3);
mv.setDrawingCacheEnabled(真);
}
//@覆盖
//RestoreInstanceState上的受保护无效(Bundle savedInstanceState){
//
//mCurrentPhotoPath=(文件)savedInstanceState.getSerializable(我的当前照片路径键);
//super.onRestoreInstanceState(savedInstanceState);
// }
//
//@覆盖
//SaveInstanceState上受保护的无效(束超出状态){
//
//outState.putSerializable(MY_CURRENT_PHOTO_PATH_KEY,McCurrentPhotoPath);
//super.onSaveInstanceState(超出状态);
// }
私有void setListeners(){
save.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
如果(!mv.isSigned()){
Toast.makeText(SignatureActivity.this,R.string.empty_sign,Toast.LENGTH_SHORT).show();
}否则{
SimpleDataFormat sdf=新的SimpleDataFormat(“yyyyMMddHHmmss”,Locale.US);
字符串currentDateandTime=sdf.format(new Date());
String name=“sign_u”+currentDateandTime;
位图Bitmap=mv.getDrawingCache();
File imagesFolder=getExternalFilesDir(Environment.DIRECTORY\u图片);
imagesFolder.mkdirs();
File File=新文件(imagesFolder,name+“.jpg”);
试一试{
如果(!file.exists()){
createNewFile();
}
FileOutputStream ostream=新的FileOutputStream(文件);
compress(bitmap.CompressFormat.JPEG,80,ostream);
ostream.close();
mv.invalidate();
}捕获(例外e){
e、 printStackTrace();
}最后{
mv.setDrawingCacheEnabled(真);
}
意向数据=新意向();
data.putExtra(getPackageName()+“_signaturePath”,file.getPath());
设置结果(结果正常,数据);
finishWithResult(file.getPath());
if(签名!=null){
文件file1=新文件(签名);
file1.delete();
}
}
}
});
clear.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
mv.clear();
mv.setIsSigned(假);
}
});
}
私有void finishWithResult(字符串路径){
意图=新意图();
设置结果(结果正常,意图);
intent.putExtra(“路径”,路径);
完成();
}
@凌驾
公共布尔句柄消息(消息消息消息){
返回false;
}
公共类DroperView扩展视图{
私有位图mBitmap;
私人帆布mCanvas;
专用路径mPath;
私人油漆;
私人浮动mX、mY、sX、sY;
private boolean isSigned=false;
私有静态最终字符串EXTRA\u EVENT\u LIST=“EVENT\u LIST”;
私有静态最终字符串EXTRA_STATE=“instance_STATE”;
//私有ArrayList事件列表=新ArrayList(100);
公共抽屉视图(上下文c){
超级(c);
mPath=新路径();
mBitmapPaint=新油漆(油漆抖动标志);
mBitmapPaint.setColor(Color.WHITE);
setSaveEnabled(真);
}
公共抽屉视图(上下文c、位图){
超级(c);
android:configChanges="orientation|screenSize"
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize"/>
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//Handle rotation from landscape to portarit mode here
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//Handle rotation from portrait to landscape mode here
}
}
<activity
android:name=".SignatureActivity"
android:label="@string/title_yourTitle"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"/>