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

来源:互联网 发布:怎么查询淘宝授权书 编辑:程序博客网 时间:2024/06/14 05:01

图片视图(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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubString 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);}@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {// TODO Auto-generated method stubswitch (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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sweetlover.imageview"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name="com.sweetlover.activity.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application></manifest>

0 0