码迷,mamicode.com
首页 > 移动开发 > 详细

阅读《Android 从入门到精通》(20)——图片视图

时间:2016-02-01 15:53:54      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

图片视图(ImageView)

ImageView 类属于 android.Widget 包并且继承于 android.widget.View 类,派生了 ImageButton 和 ZoomButton 等子类,主要用于对图片作相关处理。可以通过 setImageBitmap 方法或 setImageResource(int) 方法设置图片资源,或者通过 android:src 属性指定。

ImageView 类方法

技术分享
技术分享

ImageView 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9424612

下述程序主要学习 ImageView、Bitmap 以及 BitmapFactory 的用法,其中要注意 ImageView 的方法需要在主线程中进行,不可以在工作者线程中执行,否则会引发崩溃,BitmapFactory 的作用是图像解码,Bitmap 相当于是解码后的位图句柄,ImageView 相当于呈现容器。

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.imageview.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Bitmap bitmap = null;
	private TextView textView = null;
	private ImageView imageView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		textView = (TextView) findViewById(R.id.textView1);
		imageView = (ImageView) findViewById(R.id.imageView1);

		textView.setText("按 1 放大\t按 2 缩小\n按 3 左转\t按 4 右转");
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.miku);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		String text;
		switch (keyCode) {
		case KeyEvent.KEYCODE_1:
			text = "正在放大图片";
			break;
		case KeyEvent.KEYCODE_2:
			text = "正在缩小图片";
			break;
		case KeyEvent.KEYCODE_3:
			text = "正在左转图片";
			break;
		case KeyEvent.KEYCODE_4:
			text = "正在右转图片";
			break;
		default:
			text = "无效按键";
			break;
		}
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_1:
			ZoomImageView(2.0f);
			break;
		case KeyEvent.KEYCODE_2:
			ZoomImageView(0.5f);
			break;
		case KeyEvent.KEYCODE_3:
			RotateImageView(-10);
			break;
		case KeyEvent.KEYCODE_4:
			RotateImageView(10);
			break;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	public void ZoomImageView(float rate) {
		Bitmap bmp;
		Matrix matrix;
		int width, height;
		
		matrix = new Matrix();
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		matrix.postScale(rate, rate);
		bmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		imageView.setImageBitmap(bmp);
	}
	
	public void RotateImageView(int degree) {
		Bitmap bmp;
		Matrix matrix;
		int width, height;
		
		matrix = new Matrix();
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		matrix.postScale(1f, 1f);
		matrix.setRotate(degree);
		bmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		imageView.setImageBitmap(bmp);
	}
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="30dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"
        android:src="@drawable/miku" />
    
</LinearLayout>

3.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="30dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name"
        android:src="@drawable/miku" />
    
</LinearLayout>

阅读《Android 从入门到精通》(20)——图片视图

标签:

原文地址:http://blog.csdn.net/sweetloverft/article/details/50617086

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!