WindowManager 変数 = [Activity].getWindowManager();まず最初に行うのは「WindowManager」というクラスを取得することです。これは文字通りウインドウを管理するためのもので、Activityにある「getWindowManager」メソッドで得ることができます。
Display 変数 = [WindowManager].getDefaultDisplay();次に、画面を管理するための「Display」クラスを取得します。これはWindowManagerの「getDefaultDisplay」を使って取り出します。
DisplayMetrics 変数 = new DisplayMetrics();準備が整ったところで、DisplayMetricsを用意しましょう。これは、そのままnewでインスタンスを作成します。ただし、この状態では、DisplayMetricsには必要な情報は入っていません。
[Display].getMetrics( [DisplayMetrics] );最後に、Displayの「getMetrics」を使い、DisplayMetricsインスタンスに情報を取り出します。このメソッドは、引数に用意したDisplayMetricsインスタンスに必要な情報を設定するものです。これで、DisplayMetricsインスタンスに表示画面の情報が保管されました。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
package jp.tuyano;
import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.*;
public class SampleApp extends Activity {
DisplayMetrics metrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
DrawView view = new DrawView(getApplication());
setContentView(view);
WindowManager manager = this.getWindowManager();
Display display = manager.getDefaultDisplay();
metrics = new DisplayMetrics();
display.getMetrics(metrics);
}
class DrawView extends View {
public DrawView(Context context) {
super(context);
}
public void onDraw(Canvas c) {
int w = metrics.widthPixels;
int h = metrics.heightPixels;
float d = 100f * metrics.density;
c.drawColor(Color.BLACK);
Paint fill_paint = new Paint();
fill_paint.setStyle(Paint.Style.FILL);
fill_paint.setColor(Color.RED);
c.drawOval(new RectF(0f, 0f, d, d), fill_paint);
c.drawOval(new RectF(w - d, 0f, (float)w, d), fill_paint);
c.drawOval(new RectF(0f, h - d, d, (float)h), fill_paint);
c.drawOval(new RectF(w - d, h - d, (float)w, (float)h), fill_paint);
}
}
}
| << 前へ |