【android】可放大缩小图片位置点击位置获取

直接上代码。
<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() {
			
			@Override
			public 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() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				System.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
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(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);
	}
}



地图

猜你喜欢

转载自duduli.iteye.com/blog/1695452