初学Android、グラフィック画像のMatrix(二十九)
Matrixクラスでは、グラフィックのパン、回転、ズーム、チルトを制御したり、Viewコンポーネントのパン、回転、チルトなどを制御したりできます.
次の例postInvalidateメソッドは再描画され、onDrawメソッドが呼び出され、onDrawメソッドでビットマップのスケーリングとチルト操作が行われます.
MainでxmlはカスタムViewコンポーネントを参照
次の例postInvalidateメソッドは再描画され、onDrawメソッドが呼び出され、onDrawメソッドでビットマップのスケーリングとチルト操作が行われます.
package WangLi.Graphics.Matrix;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
public class MyView extends View {
//
private Bitmap bitmap;
//Matrix
private android.graphics.Matrix matrix = new android.graphics.Matrix();
//
private float sx = 0.0f;
//
private int width,height;
//
private float scale = 1.0f;
//
private boolean isScale = false;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//
bitmap = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.psb)).getBitmap();
//
width = bitmap.getWidth();
//
height = bitmap.getHeight();
//
this.setFocusable(true);
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
// Matrix
matrix.reset();
if(!isScale)
{
// Matrix
matrix.setSkew(sx, 0);
}
else
{
// Matrix
matrix.setScale(scale, scale);
}
// Matrix
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
//
canvas.drawBitmap(bitmap2, matrix, null);
}
public boolean onKeyDown(int KeyCode,KeyEvent event)
{
switch(KeyCode)
{
//
case KeyEvent.KEYCODE_DPAD_LEFT:
isScale = false;
sx += 0.1;
postInvalidate();
break;
//
case KeyEvent.KEYCODE_DPAD_RIGHT:
isScale = false;
sx -= 0.1;
postInvalidate();
break;
//
case KeyEvent.KEYCODE_DPAD_UP:
isScale = true;
if(scale < 2.0)
scale += 0.1;
postInvalidate();
break;
//
case KeyEvent.KEYCODE_DPAD_DOWN:
isScale = true;
if(scale > 0.5)
scale -= 0.1;
postInvalidate();
break;
}
return super.onKeyDown(KeyCode, event);
}
}
MainでxmlはカスタムViewコンポーネントを参照
<?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" >
<WangLi.Graphics.Matrix.MyView android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</LinearLayout>
以下は効果