【android】可放大缩小图片位置点击位置获取
16lz
2021-01-23
直接上代码。
在imageview外层加上ScrollView和HorizontalScrollView主要是为了图片在放大的时候使用,
地图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button" /> <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/button1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/campus_map" /> </ScrollView> </HorizontalScrollView> </RelativeLayout>
在imageview外层加上ScrollView和HorizontalScrollView主要是为了图片在放大的时候使用,
package com.example.test;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.ImageView;import android.widget.ScrollView;public class MainActivity extends Activity {private Button b;private ImageView imageView;private Matrix mMatrix;private Bitmap bmp; private float scaleWidth=1; private float scaleHeight=1; private int i = 0; private ScrollView scrollView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button) this.findViewById(R.id.button1); mMatrix = new Matrix(); imageView = (ImageView) findViewById(R.id.imageView1); bmp = ((BitmapDrawable) getResources().getDrawable( R.drawable.campus_map)).getBitmap(); scrollView = (ScrollView) this.findViewById(R.id.scrollView1); // button是放大作用,图片每次放大1.25倍。 b.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//mMatrix.setRotate(90);// mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight()); // Bitmap bm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), // bmp.getHeight(), mMatrix, true);// imageView.setImageBitmap(bm);// double scale=1.25; /* 计算这次要放大的比例 */ scaleWidth=(float)(scaleWidth*scale); scaleHeight=(float)(scaleHeight*scale); /* 产生reSize后的Bitmap对象 */ Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); imageView.setImageBitmap(resizeBmp); // }}); // 获取imageview点击处的位置。 imageView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubSystem.out.println("当前获取位置:"+event.getX()+" "+event.getY());return false;}}); // Random r = new Random();// mMatrix.setRotate(90);// mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight()); // mMatrix.postSkew(0.3f, 0.7f); mMatrix.setTranslate(bmp.getWidth(), bmp.getHeight()); Bitmap bm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mMatrix, true); imageView.setImageBitmap(bm); System.out.println("pppp"); /*mMatrix.setRotate(90); mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight()); Bitmap bm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mMatrix, true); imageView.setImageBitmap(bm); */ }// 使用activity的onTouch方法是无效的,只能使用dispatchTouchEvent@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubif(ev.getAction()==MotionEvent.ACTION_DOWN){System.out.println("imageview屏幕大小:"+imageView.getWidth()+" "+imageView.getHeight());////System.out.println(imageView.getHorizontalFadingEdgeLength());//System.out.println(imageView.getVerticalFadingEdgeLength());}return super.dispatchTouchEvent(ev);}}
地图
![【android】可放大缩小图片位置点击位置获取_第1张图片](https://img.16lz.com/image/product/aec080a782234c4abc27fd70bd696f0a.jpg)
更多相关文章
- Android 将drawable下的图片转换成bitmap、Drawable
- android 图片处理方法(整理)
- 布局文件绘制圆角矩形 背景图片
- Android sqlite数据库存取图片信息
- Android图片缩放、圆角处理以及倒影特效代码
- Android 实现九宫格、点击图片放大全屏浏览等
- Android ImageView图片浏览器(ImageView加载sd卡图片资源)的内存溢