【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);
}
}



地图
[img]http://dl.iteye.com/upload/attachment/0074/8459/3db78772-fce6-3617-89d4-5c38190accbe.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值