标签:
ImageView 类属于 android.Widget 包并且继承于 android.widget.View 类,派生了 ImageButton 和 ZoomButton 等子类,主要用于对图片作相关处理。可以通过 setImageBitmap 方法或 setImageResource(int) 方法设置图片资源,或者通过 android:src 属性指定。
完整工程:http://download.csdn.net/detail/sweetloveft/9424612
下述程序主要学习 ImageView、Bitmap 以及 BitmapFactory 的用法,其中要注意 ImageView 的方法需要在主线程中进行,不可以在工作者线程中执行,否则会引发崩溃,BitmapFactory 的作用是图像解码,Bitmap 相当于是解码后的位图句柄,ImageView 相当于呈现容器。
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); } }
<?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>
<?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>
标签:
原文地址:http://blog.csdn.net/sweetloverft/article/details/50617086