ImageView一例

参考自《疯狂android讲义》2.4节

效果如下:


当点击图上某点时,将之附近放大至下图。

布局文件:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_plus_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/plus_alpha"
            android:textSize="12sp" />

        <Button
            android:id="@+id/bt_minus_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minus_alpha"
            android:textSize="12sp" />

        <Button
            android:id="@+id/bt_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_picture"
            android:textSize="12sp" />
    </LinearLayout>
    
    <ImageView
        android:id="@+id/iv_full_pic"
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="3"
        android:src="@drawable/shuangta"
        android:contentDescription="@string/big_image"/>

        <ImageView
        android:id="@+id/iv_zoom_pic"
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_weight="1"
        android:src="@drawable/shuangta"
        android:contentDescription="@string/small_image"/>
    
</LinearLayout>

类文件:

package com.ljh.imageviewdemo;

import com.example.imageviewdemo.R;

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.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;

public class MainActivity extends Activity {
	private float alpha = 1.0f;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ImageView ivFullPic = (ImageView) findViewById(R.id.iv_full_pic);
		final ImageView ivZoomPic = (ImageView) findViewById(R.id.iv_zoom_pic);
		final Button btIncreaseAlpha = (Button) findViewById(R.id.bt_plus_alpha);
		final Button btDecreaseAlpha = (Button) findViewById(R.id.bt_minus_alpha);
		final Button btNextPic = (Button) findViewById(R.id.bt_next);

		final int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao,
				R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi };

		btNextPic.setOnClickListener(new OnClickListener() {
			private int currentImage = 2;

			@Override
			public void onClick(View v) {
				currentImage++;
				ivFullPic
						.setImageResource(images[currentImage % images.length]);
			}
		});

		btIncreaseAlpha.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (alpha > 1) {
					//在API11以后,建议使用setAlpha(float),而setAlpha(int) 已经 deprecated。前者取值范围0~1,后者取值范围0~255.
					ivFullPic.setAlpha(1.0f);
				} else
					ivFullPic.setAlpha(alpha += 0.01);
			}

		});

		btDecreaseAlpha.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (alpha < 0) {
					ivFullPic.setAlpha(0.0f);
				} else
					ivFullPic.setAlpha(alpha -= 0.01);
			}

		});
		
		
		ivFullPic.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View view, MotionEvent event)
			{
				BitmapDrawable bitmapDrawable = (BitmapDrawable) ivFullPic
						.getDrawable();
				// 获取第一个图片显示框中的位图
				Bitmap bitmap = bitmapDrawable.getBitmap();
				// bitmap图片实际大小与第一个ImageView的缩放比例
				double scale = bitmap.getWidth() / 480.0;
				// 获取需要显示的图片的开始点
				int x = (int) (event.getX() * scale);
				int y = (int) (event.getY() * scale);
				if (x + 120 > bitmap.getWidth())
				{
					x = bitmap.getWidth() - 120;
				}
				if (y + 120 > bitmap.getHeight())
				{
					y = bitmap.getHeight() - 120;
				}
				// 显示图片的指定区域
				ivZoomPic.setImageBitmap(Bitmap.createBitmap(bitmap
						, x, y, 120, 120));
				ivZoomPic.setAlpha(alpha);
				return false;
			}
		});
	}

}

几个知识点:

1、根据比例调整图像大小

        android:layout_height="0sp"
        android:layout_weight="3"

        android:layout_height="0sp"
        android:layout_weight="1"


2、注意典型的用匿名内部类作监听器的做法。

3、setAlpha(float)与setAlpha(int)的区别。

4、setImageResource(int)



posted @ 2013-10-30 23:02  eagleGeek  阅读(119)  评论(0编辑  收藏  举报