<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mac.a1.MainActivity"> <LinearLayout android:layout_width="368dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/guanweitu" /> <ImageButton android:id="@+id/keepalbum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:src="@mipmap/baocunxiangce" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
MainActivity:
package com.example.mac.a1; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.ImageButton; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Bitmap bitmap; private GestureDetector mGestureDetector;//添加手势 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageButton keepalbumTV = (ImageButton) findViewById(R.id.keepalbum); bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.guanweitu);// 获取图片 keepalbumTV.setOnClickListener(this); // 添加手势 mGestureDetector = new GestureDetector(this, new MyGestureListener(this)); } @Override public void onClick(View view) { Preservation_Utils.saveImageToGallery(MainActivity.this, bitmap); } // 触摸事件 @Override public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); } } /* 也可以在自定义的View里面设置手势识别: package noodies.blog.csdn.net; import android.content.Context; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class MyView extends View { private GestureDetector mGestureDetector; public MyView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, new MyGestureListener(context)); setLongClickable(true); this.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } }); } } 陷阱 对于自定义View,使用手势识别有两处陷阱可能会浪费你的不少时间。 1:View必须设置longClickable为true,否则手势识别无法正确工作,只会返回Down, Show, Long三种手势 2:必须在View的onTouchListener中调用手势识别,而不能像Activity一样重载onTouchEvent,否则同样手势识别无法正确工作 测试结果 下面是各种操作返回的手势序列,数值0表示触摸屏按下,1表示抬起 单击:down 0, single up 1, single conf 0 短按:down 0, show 0, single up 1 长按:down 0, show 0, long 0 双击:down 0, single up 1, double 0, double event 0, down 0, double event 1 滚动:down 0, (show 0), scrool 2... 滑动:down 0, (show 0), scrool 2..., fling 1 */
MyGestureListener: package com.example.mac.a1; import android.content.Context; import android.view.MotionEvent; import android.view.GestureDetector; import android.widget.Toast; /** * Created by mac on 2017/12/17. */ public class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private Context mContext; MyGestureListener(Context context) { mContext = context; } @Override public boolean onDown(MotionEvent e) { Toast.makeText(mContext, "DOWN " + e.getAction(), Toast.LENGTH_SHORT).show(); return false; } @Override public void onShowPress(MotionEvent e) { Toast.makeText(mContext, "SHOW " + e.getAction(), Toast.LENGTH_SHORT).show(); } @Override public boolean onSingleTapUp(MotionEvent e) { Toast.makeText(mContext, "SINGLE UP " + e.getAction(), Toast.LENGTH_SHORT).show(); return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Toast.makeText(mContext, "SCROLL " + e2.getAction(), Toast.LENGTH_SHORT).show(); return false; } @Override public void onLongPress(MotionEvent e) { Toast.makeText(mContext, "LONG " + e.getAction(), Toast.LENGTH_SHORT).show(); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Toast.makeText(mContext, "FLING " + e2.getAction(), Toast.LENGTH_SHORT).show(); return false; } @Override public boolean onDoubleTap(MotionEvent e) { Toast.makeText(mContext, "DOUBLE " + e.getAction(), Toast.LENGTH_SHORT).show(); return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { Toast.makeText(mContext, "DOUBLE EVENT " + e.getAction(), Toast.LENGTH_SHORT).show(); return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Toast.makeText(mContext, "SINGLE CONF " + e.getAction(), Toast.LENGTH_SHORT).show(); return false; } }
Preservation_Utils:
package com.example.mac.a1; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.net.Uri; import android.os.Environment; import android.util.Log; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; public class Preservation_Utils { public static void saveImageToGallery(Context context,Bitmap bitmap) { File appDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "huakong1"); if (!appDir.exists()) { appDir.mkdirs(); } String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, 100, fos); fos.close(); //保存图片后发送广播通知更新数据库 Uri uri = Uri.fromFile(file); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); } catch (Exception e) { e.printStackTrace(); } finally { //if (type == ScannerType.RECEIVER) {//onReceiver // ScannerByReceiver(context, file.getAbsolutePath()); //}// else if (type == ScannerType.MEDIA) { // ScannerByMedia(context, file.getAbsolutePath()); // } if (!bitmap.isRecycled()) { // bitmap.recycle(); } Toast.makeText(context, "保存成功" , Toast.LENGTH_SHORT).show(); } } /** Receiver**/ private static void ScannerByReceiver(Context context, String path) { context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path))); Log.v("TAG", "receiver scanner completed"); } } ScannerUtilsid:
package com.example.mac.a1; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.net.Uri; import android.os.Environment; import android.util.Log; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; /** * @author: HouSheng * * @类 说 明: 图片扫描工具类 */ public class ScannerUtilsid { // 扫描的三种方式 public static enum ScannerType {//枚举 RECEIVER, MEDIA } // 首先保存图片 public static void saveImageToGallery(Context context, Bitmap bitmap, ScannerType type) { File appDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "华控通"); if (!appDir.exists()) { // 目录不存在 则创建 appDir.mkdirs(); } String fileName = "华控通" + ".png"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, fos); // 保存bitmap至本地 fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (type == ScannerType.RECEIVER) {//onReceiver ScannerByReceiver(context, file.getAbsolutePath()); }// else if (type == ScannerType.MEDIA) { if (!bitmap.isRecycled()) { // bitmap.recycle(); 当存储大图片时,为避免出现OOM ,及时回收Bitmap System.gc(); // 通知系统回收 } Toast.makeText(context, "图片保存为" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show(); } } /** Receiver扫描更新图库图片 **/ static void ScannerByReceiver(Context context, String path) { context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path))); Log.v("TAG", "receiver scanner completed"); } }