當前位置:首頁 » 安卓系統 » androidbitmap繪制

androidbitmap繪制

發布時間: 2022-05-08 20:59:24

❶ android中drawBitMap繪制函數中的參數

表示行掃描的寬度。

❷ Android canvas.drawBitmap() 繪制圖片大小的問題

你繪圖的控制項是不是足夠大?像素密度有沒有計算進去?bitmap直接可以判斷大小的,單位是px。你打log把該輸出的輸出一下看看。

❸ android bitmap怎麼創建canvas

點陣圖是我們開發中最常用的資源,畢竟一個漂亮的界面對用戶是最有吸引力的。

1. 從資源中獲取點陣圖

可以使用BitmapDrawable或者BitmapFactory來獲取資源中的點陣圖。

當然,首先需要獲取資源:
Resources res=getResources();

復制代碼
使用BitmapDrawable獲取點陣圖

1. 使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;
2. 使用BitmapDrawable類的getBitmap()獲取得到點陣圖;
// 讀取InputStream並得到點陣圖

InputStream is=res.openRawResource(R.drawable.pic180);

BitmapDrawable bmpDraw=new BitmapDrawable(is);

Bitmap bmp=bmpDraw.getBitmap();

復制代碼
或者採用下面的方式:
BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);

Bitmap bmp=bmpDraw.getBitmap();

復制代碼
使用BitmapFactory獲取點陣圖

(Creates Bitmap objects from various sources, including files, streams, and byte-arrays.)

使用BitmapFactory類decodeStream(InputStream is)解碼點陣圖資源,獲取點陣圖。
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);

復制代碼
BitmapFactory的所有函數都是static,這個輔助類可以通過資源ID、路徑、文件、數據流等方式來獲取點陣圖。

以上方法在編程的時候可以自由選擇,在Android SDK中說明可以支持的圖片格式如下:png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。

2. 獲取點陣圖的信息

要獲取點陣圖信息,比如點陣圖大小、像素、density、透明度、顏色格式等,獲取得到Bitmap就迎刃而解了,這些信息在Bitmap的手冊中,這里只是輔助說明以下2點:

* 在Bitmap中對RGB顏色格式使用Bitmap.Config定義,僅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如說RGB_555,在開發中可能需要注意這個小問題;
* Bitmap還提供了compress()介面來壓縮圖片,不過AndroidSAK只支持PNG、JPG格式的壓縮;其他格式的需要Android開發人員自己補充了。

3. 顯示點陣圖

顯示點陣圖可以使用核心類Canvas,通過Canvas類的drawBirmap()顯示點陣圖,或者藉助於BitmapDrawable來將Bitmap繪制到Canvas。當然,也可以通過BitmapDrawable將點陣圖顯示到View中。

轉換為BitmapDrawable對象顯示點陣圖
// 獲取點陣圖

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);

// 轉換為BitmapDrawable對象

BitmapDrawable bmpDraw=new BitmapDrawable(bmp);

// 顯示點陣圖

ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);

iv2.setImageDrawable(bmpDraw);

復制代碼
使用Canvas類顯示點陣圖

這兒採用一個繼承自View的子類Panel,在子類的OnDraw中顯示
public class MainActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new Panel(this));

}class Panel extends View{

public Panel(Context context) {

super(context);

}

public void onDraw(Canvas canvas){

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);

canvas.drawColor(Color.BLACK);

canvas.drawBitmap(bmp, 10, 10, null);

}

}

}

復制代碼
4. 點陣圖縮放
(1)將一個點陣圖按照需求重畫一遍,畫後的點陣圖就是我們需要的了,與點陣圖的顯示幾乎一樣:drawBitmap(Bitmapbitmap, Rect src, Rect dst, Paint paint)。
(2)在原有點陣圖的基礎上,縮放原點陣圖,創建一個新的點陣圖:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
(3)藉助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不過要注意此時整個畫布都縮放了。
(4)藉助Matrix:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(dstbmp, 10, 10, null);

5. 點陣圖旋轉
同樣,點陣圖的旋轉也可以藉助Matrix或者Canvas來實現。
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
Matrix matrix=new Matrix();
matrix.postScale(0.8f, 0.8f);
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(dstbmp, 10, 10, null);

旋轉效果:


6.圖片水印的生成方法
生成水印的過程。其實分為三個環節:第一,載入原始圖片;第二,載入水印圖片;第三,保存新的圖片。

/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}

int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//創建一個新的和SRC長度寬度一樣的點陣圖
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐標開始畫入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存儲
return newb;
}


7.Canvas的save和restore
onDraw方法會傳入一個Canvas對象,它是你用來繪制控制項視覺界面的畫布。
在onDraw方法里,我們經常會看到調用save和restore方法,它們到底是干什麼用的呢?
❑ save:用來保存Canvas的狀態。save之後,可以調用Canvas的平移、放縮、旋轉、錯切、裁剪等操作。
❑ restore:用來恢復Canvas之前保存的狀態。防止save後對Canvas執行的操作對後續的繪制有影響。
save和restore要配對使用(restore可以比save少,但不能多),如果restore調用次數比save多,會引發Error。save和restore之間,往往夾雜的是對Canvas的特殊操作。
例如:我們先想在畫布上繪制一個右向的三角箭頭,當然,我們可以直接繪制,另外,我們也可以先把畫布旋轉90°,畫一個向上的箭頭,然後再旋轉回來(這種旋轉操作對於畫圓周上的標記非常有用)。然後,我們想在右下角有個20像素的圓,那麼,onDraw中的核心代碼是:
int px = getMeasuredWidth();
int py = getMeasuredWidth();
// Draw background
canvas.drawRect(0, 0, px, py, backgroundPaint);
canvas.save();
canvas.rotate(90, px/2, py/2);
// Draw up arrow
canvas.drawLine(px / 2, 0, 0, py / 2, linePaint);
canvas.drawLine(px / 2, 0, px, py / 2, linePaint);
canvas.drawLine(px / 2, 0, px / 2, py, linePaint);
canvas.restore();
// Draw circle
canvas.drawCircle(px - 10, py - 10, 10, linePaint);
效果如圖1所示:

如果我們不調用save和restore會是什麼樣子呢?如圖2所示:

從這兩個圖中,我們就能看到圓圈位置的明顯差異。不進行Canvas的save和restore操作的話,所有的圖像都是在畫布旋轉90°後的畫布上繪制的。當執行完onDraw方法,系統自動將畫布恢復回來。save和restore操作執行的時機不同,就能造成繪制的圖形不同。

❹ Android 怎麼使用Bitmap+Canvas 自適應屏幕

我們可以使用Matrix 來放縮我們得到的Bitmap 從而使我們的BItmap適應我們的手機屏幕

首先我們得先獲取我們的手機屏幕的大小

java">WindowManagerwm=(WindowManager)getContext().getSystemService(
Context.WINDOW_SERVICE);
intwidth=wm.getDefaultDisplay().getWidth();
intheight=wm.getDefaultDisplay().getHeight();

然後我們構造一個新的Matrix對象,自己完成寫一個函數,如下:

publicBitmapresizeBitmap(Bitmapbitmap,intw,inth)
{
if(bitmap!=null)
{
intwidth=bitmap.getWidth();
intheight=bitmap.getHeight();
intnewWidth=w;
intnewHeight=h;
floatscaleWight=((float)newWidth)/width;
floatscaleHeight=((float)newHeight)/height;
Matrixmatrix=newMatrix();
matrix.postScale(scaleWight,scaleHeight);
Bitmapres=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
returnres;

}
else{
returnnull;
}
}

這樣我們通過這個函數返回的Bitmap對象就是可以適應我們手機屏幕大小的了。。

❺ android是否可以以畫圖的形式將圖片畫在某位置

可以。

1、在View的onDraw中獲取canvas

@Override
protectedvoidonDraw(Canvascanvas){//onDraw中獲取參數中的canvas
//TODOAuto-generatedmethodstub
super.onDraw(canvas);
}

2、獲取圖片,轉化為Bitmap對象

//從資源文件中生成點陣圖bitmap
Bitmapbitmap=BitmapFactory.decodeResource(getResources(),R.drawable.icon);

3、通過canvas的drawbitmap方法,把圖片畫到任意位置。

//Bitmap:圖片對象,left:偏移左邊的位置,top:偏移頂部的位置
//rawBitmap(Bitmapbitmap,floatleft,floattop,Paintpaint)
canvas.drawBitmap(bitmap,10,60,paint);//在10,60處開始繪制圖片

❻ android怎麼生成bitmap

1、

[java] view plain
public Bitmap convertViewToBitmap(View view){

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
//利用bitmap生成畫布
Canvas canvas = new Canvas(bitmap);

//把view中的內容繪制在畫布上
view.draw(canvas);

return bitmap;
}

2、

[java] view plain
/**
* save view as a bitmap
*/
private Bitmap saveViewBitmap(View view) {
// get current view bitmap
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache(true);

Bitmap bmp = plicateBitmap(bitmap);
if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; }
// clear the cache
view.setDrawingCacheEnabled(false);
return bmp;
}

public static Bitmap plicateBitmap(Bitmap bmpSrc)
{
if (null == bmpSrc)
{ return null; }

int bmpSrcWidth = bmpSrc.getWidth();
int bmpSrcHeight = bmpSrc.getHeight();

Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888); if (null != bmpDest) { Canvas canvas = new Canvas(bmpDest); final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);

canvas.drawBitmap(bmpSrc, rect, rect, null); }

return bmpDest;
}

❼ android怎樣將canvas繪制的圖形保存到bitmap中

可以用Bitmap.compress函數來把Bitmap對象保存成PNG或JPG文件,然後可以用BitmapFactory把文件中的數據讀進來再生成Bitmap對象。
保存的代碼大概類似於這樣:
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
具體的可以去查Bitmap和BitmapFactory的幫助文檔。

❽ Android開發里怎麼在surfaceView里給Bitmap對象添加Animation動畫

你好!
在run()方法里添加
draw()方法,在draw()方法里繪制,bitmap繪製成動畫,你首先需要幾張動作的圖,然後按照計時器的方式每次畫不同的圖,連接起來就是一個動畫了,比如有三張圖把,先設置計時器
int
count
=
0
;
count++;
if(count
<
30){
canvas.drawBitmap();
畫第一張圖
}
if(count
<
50){
畫第二張圖
}
。。。大概就是這樣
細節你自己琢磨把
僅代表個人觀點,不喜勿噴,謝謝。

❾ 誰能幫我詳細講講android里的Bitmap的用法啊,結合Canvas和Drawable,謝了

Bitmap:表示繪制圖像的點陣(矩陣),繪制的目的地、目標
canvas畫布:持有繪圖方法的對象(繪制動作)
例如:
//從drawable(在res目錄下)里的圖片轉到輸入流
InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code)
//將輸入流解碼為bitmap對象
mBitmap = BitmapFactory.decodeStream(is);
//然後,在view的onDraw中將此bitmap對象繪制到屏幕的某一位置
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);

Paint p = new Paint();
float y = 10;

p.setColor(Color.RED);
canvas.drawBitmap(mBitmap, 10, y, p);
y += mBitmap.getHeight() + 10;
canvas.drawBitmap(mBitmap2, 10, y, p);
y += mBitmap2.getHeight() + 10;
p.setShader(mShader);
canvas.drawBitmap(mBitmap3, 10, y, p);
}

❿ android 能在bitmap上繪制圓嗎

能,這是往圖片(圖片轉為Bitmap)上寫入文字、圖片,你換成畫圓就是得了。
/**
* 往圖片上寫入文字、圖片等內容
*/
private void drawNewBitmap(String str) {
Bitmap photo = BitmapFactory.decodeResource(this.getResources(),R.drawable.introce_first);

int width = photo.getWidth();
int hight = photo.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); // 建立一個空的BItMap
Canvas canvas = new Canvas(bitmap);// 初始化畫布繪制的圖像到icon上

Paint photoPaint = new Paint(); // 建立畫筆
photoPaint.setDither(true); // 獲取跟清晰的圖像采樣
photoPaint.setFilterBitmap(true);// 過濾一些

Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());// 創建一個指定的新矩形的坐標
Rect dst = new Rect(0, 0, width, hight);// 創建一個指定的新矩形的坐標
canvas.drawBitmap(photo, src, dst, photoPaint);// 將photo 縮放或則擴大到
// dst使用的填充區photoPaint

Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);// 設置畫筆
textPaint.setTextSize(30.0f);// 字體大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);// 採用默認的寬度
textPaint.setColor(Color.BLACK);// 採用的顏色
canvas.drawText(str, 200, 200, textPaint);// 繪制上去字,開始未知x,y採用那隻筆繪制
canvas.drawBitmap(BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher), 100, 100, textPaint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
iv.setBackgroundDrawable(new BitmapDrawable(this.getResources(), bitmap));
saveMyBitmap(bitmap,"test1");
}

熱點內容
加密殼sdk 發布:2025-05-12 07:38:29 瀏覽:508
電腦網線通伺服器 發布:2025-05-12 07:34:59 瀏覽:679
訪問法概念 發布:2025-05-12 07:27:14 瀏覽:406
遺傳演算法例子 發布:2025-05-12 07:27:11 瀏覽:266
matlab語言編程 發布:2025-05-12 07:05:16 瀏覽:482
解壓油畫棒 發布:2025-05-12 06:56:56 瀏覽:716
如何安裝語言編譯器 發布:2025-05-12 06:55:05 瀏覽:300
c語言程序設計題目 發布:2025-05-12 06:46:46 瀏覽:712
虛擬機上傳文件 發布:2025-05-12 06:41:52 瀏覽:572
編程模特 發布:2025-05-12 06:41:51 瀏覽:271