標籤:接下來 建立 str html 映像 dia 簡單 pac too
android.graphics.Bitmap(位元影像)是Android手機中專門提供的用於操作圖片資源的操作類,使用此類能夠直接從資源檔之中進行圖片資源的讀取。而且對這些圖片進行一些簡單的改動。
經常使用的方法
| 1 |
public static Bitmap createBitmap (Bitmap src) |
普通 |
複製一個Bitmap |
| 2 |
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) |
普通 |
對一個Bitmap進行剪下 |
| 3 |
public final int getHeight() |
普通 |
取得映像的高 |
| 4 |
public final int getWidth() |
普通 |
取得映像的寬 |
| 5 |
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) |
普通 |
建立一個指定大小的Bitmap |
接下來用簡單的範例來進行說明
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.example.bitmap1.MyView android:layout_width="wrap_content" android:layout_height="wrap_content"></com.example.bitmap1.MyView></RelativeLayout>
主程式
package com.example.bitmap1;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
MyView中定義的bitmap
package com.example.bitmap1; import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class MyView extends View {public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stub//擷取圖片檔案Bitmap bitmap = BitmapFactory.decodeResource(super.getResources(),R.drawable.a4);//設定背景畫布顏色canvas.drawColor(Color.WHITE);//初始化畫筆Paint paint=new Paint();//設定邊緣羽化paint.setAntiAlias(true);//繪製圖片canvas.drawBitmap(bitmap, 0, 0,paint );//設定畫筆顏色paint.setColor(Color.BLUE);//設定字型尺寸paint.setTextSize(20);//繪製文字canvas.drawText("我的頭像", 10, bitmap.getHeight()+20, paint);}}
儘管有點醜。可是繪製的還不錯。對bitmap的操作還有很多,大家能夠參照api自行進行學習,圖形的繪製在遊戲和APP引導用的比較多
下節預報:Mediaplayer內建播放器
從零開始學android<Bitmap圖形組件.四十七.>