Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> android用canvas和ImageView的圖片顯示

android用canvas和ImageView的圖片顯示

編輯:初級開發

package com.ray.draw; import Java.io.InputStream; import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.view.VIEw; 
//用畫布的形式顯示
public class TestDrawBitmap extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(new myVIEw(this)); 

}

class myView extends View{ public myVIEw(Context context) { 
super(context); 

protected void onDraw(Canvas canvas) { 
super.onDraw(canvas); 
//read the icon.png into buffer 
InputStream is = getResources().openRawResource(R.drawable.icon); 
//decode 
Bitmap mBitmap = BitmapFactory.decodeStream(is); 
Paint mPaint = new Paint(); 
canvas.drawBitmap(mBitmap, 40, 40, mPaint); 
} }

------------------------------------------------用imageVIEw顯示

/**
* 從URL中取得Bitmap,並限制小於指定大小時才載入. 默認200K
* @param strurl URL全路徑
* @return
*/
public static Bitmap getURLBitmap(String strurl)
{
int defaultLargestSize =200 * 1024; // 默認200K
URL imageUrl = null;
Bitmap bitmap = null;
try{
imageUrl = new URL(strurl);
} catch (MalformedURLException e){
e.printStackTrace();
}
try{
HttpGet httpRequest = new HttpGet(imageUrl.toURI());
HttpClient httpclient = new DefaultHttpClIEnt();
HttpResponse response = (HttpResponse) httpclIEnt.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity buffHttpEntity = new BufferedHttpEntity(entity);
if (buffHttpEntity.getContentLength() < defaultLargestSize){
InputStream is = buffHttpEntity.getContent();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}else{
bitmap = null;
}
} catch (Exception e){
e.printStackTrace();
}return bitmap;
}

//實際調用

private Bitmap bmp =null;

ImageView mImageVIEw1;

bmp = Global.getURLBitmap(uriPic+photo_path);
if (bmp == null){
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 2; 
bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.notice);
}

mImageVIEw1.setImageBitmap(bmp);

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved