Using the zoom factor to zoom in and out of the picture, the effect picture is as follows
In the case of direct crashes during implementation: add __android:largeHeap=”true” ** to the global configuration file to solve the problem, there is ready code at the end of the article
Steps:
Android :scaleType=”matrix
2. Control instantiation and replacement of processing mechanism
3. Carry out logical processing, and use Matrix for image processing支那Where nowScale and beforeScale are custom global variables of type float! , see the last code for details支那
The code is as follows:
Layout file
<ImageView
android:id="@+id/imgzoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="matrix"
android:src="@drawable/myphoto" />
Copy the code
Java file
public class MainActivity extends Activity { private ImageView mImgzoom; Private float beforeScale = 1.0 f; Private float nowScale; @override protected void onCreate(Bundle savedInstanceState) {super.oncreate (savedInstanceState); setContentView(R.layout.activity_main); initView(); myScale(); } private void initView() { mImgzoom = (ImageView) findViewById(R.id.imgzoom); } private void myScale(){ final ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector(this, new ScaleGestureDetector.OnScaleGestureListener() { @Override public boolean onScale(ScaleGestureDetector detector) { nowScale=detector.getScaleFactor()*beforeScale; / / set the zoom range if (nowScale > | 3 | nowScale < 0.1) {beforeScale = nowScale; return true; } Log.i("Scale","nowScale="+nowScale); Matrix matrix=new Matrix(); matrix.setScale(nowScale,nowScale); Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.myphoto); / / get photo bitmap = bitmap. CreateBitmap (bitmap, 0, 0, bitmap. The getWidth (), bitmap. GetHeight (), matrix, true); // Convert to bitmap format mimgzoom.setimageBitmap (bitmap); beforeScale=nowScale; // Save the last scaling value! return false; } @Override public boolean onScaleBegin(ScaleGestureDetector detector) { return true; // Remember to change to true; } @Override public void onScaleEnd(ScaleGestureDetector detector) { } }); mImgzoom.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { scaleGestureDetector.onTouchEvent(event); return true; }}); }}Copy the code