阅读《Android 从入门到精通》(20)——图片视图
Posted SweetLoverFT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《Android 从入门到精通》(20)——图片视图相关的知识,希望对你有一定的参考价值。
图片视图(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
<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>
以上是关于阅读《Android 从入门到精通》(20)——图片视图的主要内容,如果未能解决你的问题,请参考以下文章