欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中Bitmap用法實例分析

 更新時間:2016年02月01日 09:13:50   作者:馬到成功  
這篇文章主要介紹了Android中Bitmap用法,結(jié)合實例形式分析了Android操作圖片的載入、屬性設(shè)置、旋轉(zhuǎn)等相關(guān)技巧,需要的朋友可以參考下

本文實例講述了Android中Bitmap用法。分享給大家供大家參考,具體如下:

一般在android程序中把圖片文件放在res/drawable目錄下就可以通過R.drawable.id來使用,但在存儲卡中的圖片怎樣引用呢?下面通過實現(xiàn)這個功能來介紹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";這種寫法是錯誤的,路徑不是以
//設(shè)備開頭
  /** 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()){
//如果存在,通過Bitmap將圖片放入ImageView中顯示出來
/*BitmapFactory(Android.graphics.BitmapFactory)是Android API提供的對象,該對象
*的decodeFile()方法將手機中的圖片文件轉(zhuǎn)換成Bitmap對象。*/
   Bitmap bm=BitmapFactory.decodeFile(fileName);
   iv.setImageBitmap(bm);
   tv.setText(fileName);
  }
  else{
   tv.setText("文件不存在");
  }
  }
  });
  }
}

BitmapFactory也提供了其他方法,例如decodeResource()可以將res/drawable內(nèi)預(yù)先存入的圖片文件轉(zhuǎn)換成Bitmap對象,decodeStream()方法可將InputStream轉(zhuǎn)化成Bitmap對象。 

下面這個例子是利用Matrix.setRotate()方法來實現(xiàn)ImageView的旋轉(zhuǎn)。原理是將ImageView放入Bitmap中,然后利用Bitmap.createBitmap()方法來創(chuàng)建新的Bitmap對象,在創(chuàng)建的同時,Matrix對象里的setRotate()方法動態(tài)旋轉(zhuǎn)新創(chuàng)建的Bitmap.然后將旋轉(zhuǎn)好的Bitmap對象以新構(gòu)造的方式創(chuàng)建新的Bitmap,并將其放入原來的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開發(fā)入門與進階教程》及《Android圖形與圖像處理技巧總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • 解析Kotlin?JSON格式

    解析Kotlin?JSON格式

    這篇文章主要介紹了Kotlin?JSON格式解析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Android自動播放Banner圖片輪播效果

    Android自動播放Banner圖片輪播效果

    這篇文章主要介紹了Android自動播放Banner圖片輪播效果的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android學(xué)習(xí)之AppWidget高級效果

    Android學(xué)習(xí)之AppWidget高級效果

    這篇文章主要為大家詳細介紹了Android學(xué)習(xí)之AppWidget高級效果的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android Studio真機無線連接USB設(shè)備調(diào)試運行詳解流程

    Android Studio真機無線連接USB設(shè)備調(diào)試運行詳解流程

    你在Android Studio寫app時是否也有想過如果可以不用數(shù)據(jù)線連接手機調(diào)試運行就好了?如果需要取出數(shù)據(jù)線插接的話我肯定是嫌麻煩的,但是模擬器有時候需要測試一些需要硬件支持的功能時又不管用,所以最好的測試還是在真機上,本篇教你扔掉數(shù)據(jù)線來無線調(diào)試
    2021-11-11
  • Android?App實現(xiàn)閃屏頁廣告圖的全屏顯示實例

    Android?App實現(xiàn)閃屏頁廣告圖的全屏顯示實例

    這篇文章主要為大家介紹了Android?App實現(xiàn)閃屏頁廣告圖的全屏顯示實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Android?NDK入門初識(組件結(jié)構(gòu)開發(fā)流程)

    Android?NDK入門初識(組件結(jié)構(gòu)開發(fā)流程)

    這篇文章主要為大家介紹了Android?NDK入門之初識組件結(jié)構(gòu)開發(fā)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Android中的Intent對象完全解析

    Android中的Intent對象完全解析

    這篇文章主要介紹了Android中的Intent對象,深入講解了intent對象傳遞消息的各種用法,需要的朋友可以參考下
    2016-04-04
  • MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯

    MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯

    這篇文章主要為大家介紹了MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android更多條目收縮展開控件ExpandView的示例代碼

    Android更多條目收縮展開控件ExpandView的示例代碼

    本篇文章主要介紹了Android更多條目收縮展開控件ExpandView的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android中WebView實現(xiàn)點擊超鏈接啟動QQ的方法

    Android中WebView實現(xiàn)點擊超鏈接啟動QQ的方法

    這篇文章主要給大家介紹了在Android中WebView如何實現(xiàn)點擊超鏈接啟動QQ的方法,文中給出了詳細的示例代碼,相信對大家的學(xué)習(xí)或者工作具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04

最新評論