TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
//textView1のテキストサイズの取得
String textSize1 = String.valueOf( tv1.getTextSize());
tv1.setText("getTextSize:" + textSize1); //21 pixel
tv2.setTextSize(14f);//sp
tv2.setText("setTextSize(14f)");//21/1.5
// ディスプレイ情報の取得
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
String sd = String.valueOf(metrics.scaledDensity);
tv3.setText("scaledDensity:" + sd);
2013年1月25日金曜日
文字サイズの取得と設定
2013年1月17日木曜日
画像表示:縮小拡大-Bitmap.Matrix
package com.example.blog;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Blog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomSurfaceView(this));
}
public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public CustomSurfaceView(Context context) {
super(context);
// TODO 自動生成されたコンストラクター・スタブ
setFocusable(true);
getHolder().addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO 自動生成されたメソッド・スタブ
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO 自動生成されたメソッド・スタブ
doDraw(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO 自動生成されたメソッド・スタブ
}
private void doDraw(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
// ResourceからBitmapを生成
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);// 画像のサイズ1/2
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
canvas.drawBitmap(bitmap2, 0, 0, null);
holder.unlockCanvasAndPost(canvas);
}
}
}
画像表示:一部切り取り-drawBitmap.Rect
package com.example.blog;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Blog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomSurfaceView(this));
}
public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public CustomSurfaceView(Context context) {
super(context);
// TODO 自動生成されたコンストラクター・スタブ
setFocusable(true);
getHolder().addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO 自動生成されたメソッド・スタブ
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO 自動生成されたメソッド・スタブ
doDraw(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO 自動生成されたメソッド・スタブ
}
private void doDraw(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
// ResourceからBitmapを生成
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// 描画処理
canvas.drawBitmap(bitmap, 0, 0, null);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// 描画元の矩形イメージ
Rect src = new Rect(0, 0, w/2, h/2);
// 描画先の矩形イメージ
Rect dst = new Rect(0, 100, w/2, 100 + h/2);
canvas.drawBitmap(bitmap, src, dst, null);
holder.unlockCanvasAndPost(canvas);
}
}
}
画像表示-SurfaceView
package com.example.blog;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Blog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomSurfaceView(this));
}
public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public CustomSurfaceView(Context context) {
super(context);
// TODO 自動生成されたコンストラクター・スタブ
setFocusable(true);
getHolder().addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO 自動生成されたメソッド・スタブ
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO 自動生成されたメソッド・スタブ
doDraw(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO 自動生成されたメソッド・スタブ
}
private void doDraw(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
// ResourceからBitmapを生成
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// 描画処理
canvas.drawBitmap(bitmap, 0, 0, null);
holder.unlockCanvasAndPost(canvas);
}
}
}
登録:
コメント (Atom)



