Android Bitmap 縮放 (Picture 縮放)

Android 透過 Bitmap.createScaledBitmap 來設定 Bitmap 縮放.

Java 程式範例 :
 程式碼
public class helloWorld extends Activity {

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

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( new drawCanvas(this) );
}

class drawCanvas extends View {
public drawCanvas(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

// 取得 Resource 圖片的 Bitmap
Bitmap vBitmap = BitmapFactory.decodeResource( this.getResources()
, R.drawable.icon
);

// Bitmap 縮放
Bitmap vB2 = Bitmap.createScaledBitmap( vBitmap
, vBitmap.getWidth() * 3
, vBitmap.getHeight() * 2
, true
);


canvas.drawBitmap( vBitmap, 50, 100, null ); // 無縮放
canvas.drawBitmap( vB2, 50, 200, null ); // 有縮放
}
}
}

範例結果 :


Related Posts Plugin for WordPress, Blogger...