Android中Bitmap用法實(shí)例分析
本文實(shí)例講述了Android中Bitmap用法。分享給大家供大家參考,具體如下:
一般在android程序中把圖片文件放在res/drawable目錄下就可以通過(guò)R.drawable.id來(lái)使用,但在存儲(chǔ)卡中的圖片怎樣引用呢?下面通過(guò)實(shí)現(xiàn)這個(gè)功能來(lái)介紹Bitmap的用法。
程序如下:
import java.io.File;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class A10Activity extends Activity {
private Button b;
private ImageView iv;
private TextView tv;
private String fileName="sdcard/picture/紅葉.jpg";
//private String fileName="sdcard/picture/紅葉.jpg";這種寫(xiě)法是錯(cuò)誤的,路徑不是以
//設(shè)備開(kāi)頭
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv=(ImageView)findViewById(R.id.imageview);
tv=(TextView)findViewById(R.id.textview);
File f=new File(fileName);
//先判斷圖片文件是否存在
if(f.exists()){
//如果存在,通過(guò)Bitmap將圖片放入ImageView中顯示出來(lái)
/*BitmapFactory(Android.graphics.BitmapFactory)是Android API提供的對(duì)象,該對(duì)象
*的decodeFile()方法將手機(jī)中的圖片文件轉(zhuǎn)換成Bitmap對(duì)象。*/
Bitmap bm=BitmapFactory.decodeFile(fileName);
iv.setImageBitmap(bm);
tv.setText(fileName);
}
else{
tv.setText("文件不存在");
}
}
});
}
}
BitmapFactory也提供了其他方法,例如decodeResource()可以將res/drawable內(nèi)預(yù)先存入的圖片文件轉(zhuǎn)換成Bitmap對(duì)象,decodeStream()方法可將InputStream轉(zhuǎn)化成Bitmap對(duì)象。
下面這個(gè)例子是利用Matrix.setRotate()方法來(lái)實(shí)現(xiàn)ImageView的旋轉(zhuǎn)。原理是將ImageView放入Bitmap中,然后利用Bitmap.createBitmap()方法來(lái)創(chuàng)建新的Bitmap對(duì)象,在創(chuàng)建的同時(shí),Matrix對(duì)象里的setRotate()方法動(dòng)態(tài)旋轉(zhuǎn)新創(chuàng)建的Bitmap.然后將旋轉(zhuǎn)好的Bitmap對(duì)象以新構(gòu)造的方式創(chuàng)建新的Bitmap,并將其放入原來(lái)的ImageView中。
程序如下所示:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class A11Activity extends Activity {
private ImageView iv;
private TextView tv;
private Button left,right;
private int times;
private int angle;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
times=1;
angle=1;
iv=(ImageView)findViewById(R.id.iv);
tv=(TextView)findViewById(R.id.tv);
left=(Button)findViewById(R.id.left);
left.setText("向左轉(zhuǎn)");
right=(Button)findViewById(R.id.right);
right.setText("向右轉(zhuǎn)");
final Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.a); //自己引入一張圖片a.png
final int width=bmp.getWidth();
final int height=bmp.getHeight();
iv.setImageBitmap(bmp);
left.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
angle--;
if(angle<-20){ //設(shè)置最多旋轉(zhuǎn)20度
angle=-20;
}
int width01=width*times;
int height01=height*times;
float width02=(float)(width01/width);
float height02=(float)(width02/height);
Matrix m=new Matrix();
m.postScale(width02, height02);
m.setRotate(5*angle);
Bitmap bmp01=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
BitmapDrawable bd=new BitmapDrawable(bmp01);
iv.setImageDrawable(bd);
tv.setText(Integer.toString(5*angle));
}
});
right.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
angle++;
if(angle>20){
angle=20;
}
int width01=width*times;
int height01=height*times;
float width02=(float)(width01/width);
float height02=(float)(width02/height);
Matrix m=new Matrix();
m.postScale(width02, height02);
m.setRotate(5*angle);
Bitmap bmp01=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
BitmapDrawable bd=new BitmapDrawable(bmp01);
iv.setImageDrawable(bd);
tv.setText(Integer.toString(5*angle));
}
});
}
}
res/layout/main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》及《Android圖形與圖像處理技巧總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
完美解決Android App啟動(dòng)頁(yè)有白屏閃過(guò)的問(wèn)題
這篇文章主要介紹了完美解決Android App啟動(dòng)頁(yè)有白屏閃過(guò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路
這篇文章主要介紹了Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路,需要的朋友可以參考下2018-04-04
Android 通過(guò)webservice上傳多張圖片到指定服務(wù)器詳解
這篇文章主要介紹了Android 通過(guò)webservice上傳多張圖片到指定服務(wù)器詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android透明化和沉浸式狀態(tài)欄實(shí)踐及源碼分析
這篇文章主要介紹了Android透明化和沉浸式狀態(tài)欄實(shí)踐及源碼分析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android開(kāi)發(fā)中優(yōu)秀的app 異常處理機(jī)制
這篇文章主要介紹了Android開(kāi)發(fā)中優(yōu)秀的app 異常處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
這篇文章主要介紹了Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
android選項(xiàng)卡TabHost功能用法詳解
這篇文章主要為大家詳細(xì)介紹了android選項(xiàng)卡TabHost的功能用法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android 通過(guò)ViewHolder優(yōu)化適配器的實(shí)現(xiàn)方法(必看)
下面小編就為大家?guī)?lái)一篇Android 通過(guò)ViewHolder優(yōu)化適配器的實(shí)現(xiàn)方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

