图片视图(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>

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android上一些方法的区别和用法的注意事项
  5. android实现字体闪烁动画的方法
  6. Android(安卓)matrix 控制图片的旋转、缩放、移动
  7. Android中dispatchDraw分析
  8. Android四大基本组件介绍与生命周期
  9. Android(安卓)MediaPlayer 常用方法介绍

随机推荐

  1. Android Framework架构浅析之【近期任务
  2. Android UI 介绍1
  3. android device ID获取
  4. 在Android Studio中使用Kotlin。
  5. android 在一个应用中启动另一个应用
  6. Android修改ramdisk.img笔记
  7. android避免回复出厂设置导致数据丢失的
  8. Android编译中m、mm、mmm 详解
  9. Android 常用开源库总结(持续更新)
  10. android adb pull