正文  图形图像 > OpenGL/3D >

Android立体球painter

public class GameView extends View implements Runnable {public final static String TAG = "Example_05_03_GameView";// 声明Paint对象private Paint mPaint = null;public GameView(Con......

public class GameView extends View implements Runnable {

public final static String TAG = "Example_05_03_GameView";

// 声明Paint对象

private Paint mPaint = null;

public GameView(Context context) {

super(context);

// 构建对象

mPaint = new Paint();

// 开启线程

new Thread(this).start();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// 得到Paint的一些属性颜色、Alpha值、外框的宽度、字体尺寸

//

// GradientDrawable mDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR,

// new int[] { 0xFFFF0000, 0xFF00FF00,

// 0xFF0000FF });

// mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);

// setCornerRadii(mDrawable, 90, 90, 100, 10);

// mDrawable.draw(canvas);

// mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);

// setCornerRadii(mDrawable, 0, 0, 100, 100);

// mDrawable.draw(canvas);

//canvas.drawCircle(300, 300, 100, mPaint);

// int colorFrom = Color.BLACK;

//

// int colorTo = Color.WHITE;

RadialGradient linearGradientShader = new RadialGradient(300, 300, 100,Color.GREEN, Color.BLACK,Shader.TileMode.CLAMP);

mPaint.setShader(linearGradientShader);

canvas.drawCircle(300, 300, 100, mPaint);

}

static void setCornerRadii(GradientDrawable drawable,

float r0, float r1, float r2, float r3) {

drawable.setCornerRadii(new float[] { r0, r0, r1, r1,

r2, r2, r3, r3 });

}

public void paintTriangle (int x1, int y1, int x2, int y2, int x3, int y3, Paint mPaint,Canvas canvas){

canvas.drawLine(x1, y1, x2, y2, mPaint);

canvas.drawLine(x2, y2, x3, y3, mPaint);

canvas.drawLine(x3, y3, x1, y1, mPaint);

}

// 触笔事件

public boolean onTouchEvent(MotionEvent event) {

return true;

}

// 按键按下事件

public boolean onKeyDown(int keyCode, KeyEvent event) {

return true;

}

// 按键弹起事件

public boolean onKeyUp(int keyCode, KeyEvent event) {

return true;

}

public boolean onKeyMultiple(int KeyCode, int repeatCount, KeyEvent event) {

return true;

}

@Override

public void run() {

while (!Thread.currentThread().isInterrupted()) {

try {

Thread.sleep(100);

} catch (Exception e) {

Thread.currentThread().interrupt();

}

// 更新界面

postInvalidate();

}

}

}