java 練習
import java.util.*;
class demo1 {
public static void main(String[] args) {
int x = 123456789;
StringBuffer b = new StringBuffer();
while (x != 0) {
int t = x % 10;
x = (x - t) / 10;
b.append(t + ",");
}
System.out.println(b);
}
}
//
C:\>java demo1
9,8,7,6,5,4,3,2,1,
2019年4月13日土曜日
2019年3月31日日曜日
Android游戏编程之从零开始, 練習
Android游戏编程之从零开始, 練習
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MySurfaceView2 extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private final String T = "### MySurfaceView2";
private SurfaceHolder surfaceHolder;
private boolean flag;
private Canvas canvas;
private Paint paintA, paintB, paintC;
private Rect rectA, rectB, rectC;
private int rectAx, rectAy, rectBx, rectBy, rectCx, rectCy;
private int tag;
private int px, py;
private Region regionA, regionB, regionC;
private Bitmap bitmapA, bitmapB, bitmapC;
private Matrix matrixA, matrixB, matrixC;
private Thread th;
private int screenW, screenH;
public MySurfaceView2(Context context) {
super(context);
surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
th = new Thread(this);
th.start();
init();
}
private void init() {
screenH = getHeight();
screenW = getWidth();
TGA();
TGB();
TGC();
}
private void TGA() {
bitmapA = BitmapFactory.decodeResource(getResources(), R.mipmap.a);
rectAx = 0;
rectAy = 0;
paintA = new Paint();
rectA = new Rect();
rectA.set(rectAx, rectAy, rectAx + bitmapA.getWidth(), rectAy + bitmapA.getHeight());
regionA = new Region();
regionA.op(rectA, Region.Op.XOR);
matrixA = new Matrix();
}
private void TGB() {
bitmapB = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
rectBx = this.getWidth() - bitmapB.getWidth();
rectBy = 0;
paintB = new Paint();
rectB = new Rect();
rectB.set(rectBx, rectBy, rectBx + bitmapB.getWidth(), rectBy + bitmapB.getHeight());
regionB = new Region();
regionB.op(rectA, Region.Op.DIFFERENCE);
matrixB = new Matrix();
}
private void TGC() {
bitmapC = BitmapFactory.decodeResource(getResources(), R.mipmap.c);
rectCx = 0;
rectCy = this.getHeight() - bitmapC.getHeight();
paintC = new Paint();
rectC = new Rect();
rectC.set(rectCx, rectCy, rectCx + bitmapC.getWidth(), rectCy + bitmapC.getHeight());
regionC = new Region();
regionC.op(rectC, Region.Op.REVERSE_DIFFERENCE);
matrixC = new Matrix();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
tag = 0;
break;
case MotionEvent.ACTION_MOVE:
switch (tag) {
case 1:
rectAx = (int) event.getX() + px;
rectAy = (int) event.getY() + py;
break;
case 2:
rectBx = (int) event.getX() + px;
rectBy = (int) event.getY() + py;
break;
case 3:
rectCx = (int) event.getX() + px;
rectCy = (int) event.getY() + py;
break;
default:
}
break;
case MotionEvent.ACTION_DOWN: {
if (regionA.contains((int) event.getX(), (int) event.getY())) {
px = (int) (rectA.left - event.getX());
py = (int) (rectA.top - event.getY());
tag = 1;
}
if (regionB.contains((int) event.getX(), (int) event.getY())) {
px = (int) (rectB.left - event.getX());
py = (int) (rectB.top - event.getY());
tag = 2;
}
if (regionC.contains((int) event.getX(), (int) event.getY())) {
px = (int) (rectC.left - event.getX());
py = (int) (rectC.top - event.getY());
tag = 3;
}
break;
}
default:
}
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
private void T1(Canvas canvas) {
canvas.drawColor(Color.CYAN);
mA(canvas);
mB(canvas);
mC(canvas);
}
private void mA(Canvas canvas) {
matrixA.postTranslate(rectAx, rectAy);
rectA.set(rectAx, rectAy, rectAx + bitmapA.getWidth(), rectAy + bitmapA.getHeight());
regionA.set(rectA);
paintA.setAlpha(0);
canvas.drawRect(rectA, paintA);
paintA.setAlpha(255);
canvas.drawBitmap(bitmapA, matrixA, paintA);
matrixA.reset();
}
private void mB(Canvas canvas) {
matrixB.postTranslate(rectBx, rectBy);
rectB.set(rectBx, rectBy, rectBx + bitmapB.getWidth(), rectBy + bitmapB.getHeight());
regionB.set(rectB);
canvas.drawRect(rectB, paintB);
canvas.drawBitmap(bitmapB, matrixB, paintB);
matrixB.reset();
}
private void mC(Canvas canvas) {
matrixC.postTranslate(rectCx, rectCy);
rectC.set(rectCx, rectCy, rectCx + bitmapC.getWidth(), rectCy + bitmapC.getHeight());
regionC.set(rectC);
canvas.drawRect(rectC, paintC);
canvas.drawBitmap(bitmapC, matrixC, paintC);
matrixC.reset();
}
private void MyDraw() {
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
T1(canvas);
}
} catch (Exception e) {
Log.i(T, "MyDraw Exception");
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void run() {
while (true) {
long s = System.currentTimeMillis();
MyDraw();
long e = System.currentTimeMillis();
if (50 - (e - s) > 0) {
try {
Thread.sleep(50 - (e - s));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MySurfaceView2 extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private final String T = "### MySurfaceView2";
private SurfaceHolder surfaceHolder;
private boolean flag;
private Canvas canvas;
private Paint paintA, paintB, paintC;
private Rect rectA, rectB, rectC;
private int rectAx, rectAy, rectBx, rectBy, rectCx, rectCy;
private int tag;
private int px, py;
private Region regionA, regionB, regionC;
private Bitmap bitmapA, bitmapB, bitmapC;
private Matrix matrixA, matrixB, matrixC;
private Thread th;
private int screenW, screenH;
public MySurfaceView2(Context context) {
super(context);
surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
th = new Thread(this);
th.start();
init();
}
private void init() {
screenH = getHeight();
screenW = getWidth();
TGA();
TGB();
TGC();
}
private void TGA() {
bitmapA = BitmapFactory.decodeResource(getResources(), R.mipmap.a);
rectAx = 0;
rectAy = 0;
paintA = new Paint();
rectA = new Rect();
rectA.set(rectAx, rectAy, rectAx + bitmapA.getWidth(), rectAy + bitmapA.getHeight());
regionA = new Region();
regionA.op(rectA, Region.Op.XOR);
matrixA = new Matrix();
}
private void TGB() {
bitmapB = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
rectBx = this.getWidth() - bitmapB.getWidth();
rectBy = 0;
paintB = new Paint();
rectB = new Rect();
rectB.set(rectBx, rectBy, rectBx + bitmapB.getWidth(), rectBy + bitmapB.getHeight());
regionB = new Region();
regionB.op(rectA, Region.Op.DIFFERENCE);
matrixB = new Matrix();
}
private void TGC() {
bitmapC = BitmapFactory.decodeResource(getResources(), R.mipmap.c);
rectCx = 0;
rectCy = this.getHeight() - bitmapC.getHeight();
paintC = new Paint();
rectC = new Rect();
rectC.set(rectCx, rectCy, rectCx + bitmapC.getWidth(), rectCy + bitmapC.getHeight());
regionC = new Region();
regionC.op(rectC, Region.Op.REVERSE_DIFFERENCE);
matrixC = new Matrix();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
tag = 0;
break;
case MotionEvent.ACTION_MOVE:
switch (tag) {
case 1:
rectAx = (int) event.getX() + px;
rectAy = (int) event.getY() + py;
break;
case 2:
rectBx = (int) event.getX() + px;
rectBy = (int) event.getY() + py;
break;
case 3:
rectCx = (int) event.getX() + px;
rectCy = (int) event.getY() + py;
break;
default:
}
break;
case MotionEvent.ACTION_DOWN: {
if (regionA.contains((int) event.getX(), (int) event.getY())) {
px = (int) (rectA.left - event.getX());
py = (int) (rectA.top - event.getY());
tag = 1;
}
if (regionB.contains((int) event.getX(), (int) event.getY())) {
px = (int) (rectB.left - event.getX());
py = (int) (rectB.top - event.getY());
tag = 2;
}
if (regionC.contains((int) event.getX(), (int) event.getY())) {
px = (int) (rectC.left - event.getX());
py = (int) (rectC.top - event.getY());
tag = 3;
}
break;
}
default:
}
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
private void T1(Canvas canvas) {
canvas.drawColor(Color.CYAN);
mA(canvas);
mB(canvas);
mC(canvas);
}
private void mA(Canvas canvas) {
matrixA.postTranslate(rectAx, rectAy);
rectA.set(rectAx, rectAy, rectAx + bitmapA.getWidth(), rectAy + bitmapA.getHeight());
regionA.set(rectA);
paintA.setAlpha(0);
canvas.drawRect(rectA, paintA);
paintA.setAlpha(255);
canvas.drawBitmap(bitmapA, matrixA, paintA);
matrixA.reset();
}
private void mB(Canvas canvas) {
matrixB.postTranslate(rectBx, rectBy);
rectB.set(rectBx, rectBy, rectBx + bitmapB.getWidth(), rectBy + bitmapB.getHeight());
regionB.set(rectB);
canvas.drawRect(rectB, paintB);
canvas.drawBitmap(bitmapB, matrixB, paintB);
matrixB.reset();
}
private void mC(Canvas canvas) {
matrixC.postTranslate(rectCx, rectCy);
rectC.set(rectCx, rectCy, rectCx + bitmapC.getWidth(), rectCy + bitmapC.getHeight());
regionC.set(rectC);
canvas.drawRect(rectC, paintC);
canvas.drawBitmap(bitmapC, matrixC, paintC);
matrixC.reset();
}
private void MyDraw() {
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
T1(canvas);
}
} catch (Exception e) {
Log.i(T, "MyDraw Exception");
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void run() {
while (true) {
long s = System.currentTimeMillis();
MyDraw();
long e = System.currentTimeMillis();
if (50 - (e - s) > 0) {
try {
Thread.sleep(50 - (e - s));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
ラベル:
Android游戏编程之从零开始
2019年3月28日木曜日
Math.random()
Math.random()
指定范围内乱数。
import java.util.*;
class random {
public static void main(String[] args) {
double temp = Math.random();
System.out.println(temp);
System.out.println(temp * (500 - 600));
System.out.println((int)(temp * (500 - 600) + 600));
System.out.println(Math.random() * (5 - 10) + 10);
}
}
指定范围内乱数。
import java.util.*;
class random {
public static void main(String[] args) {
double temp = Math.random();
System.out.println(temp);
System.out.println(temp * (500 - 600));
System.out.println((int)(temp * (500 - 600) + 600));
System.out.println(Math.random() * (5 - 10) + 10);
}
}
2019年3月27日水曜日
Android游戏编程之从零开始、p190
Android游戏编程之从零开始、p190
4.14.1 矩形碰撞
public boolean isCollsion(Rect rectA, Rect rectB) {
// left
// A B
if (rectA.left < rectB.left && rectA.right < rectB.left) {
return false;
}
// top
// A
// B
if (rectA.bottom < rectB.top && rectA.bottom < rectB.bottom) {
return false;
}
// right
// B A
if (rectA.left > rectB.left && rectA.left > rectB.right) {
return false;
}
// bottom
// B
// A
if (rectA.top > rectB.bottom && rectA.bottom > rectB.bottom) {
return false;
}
return true;
}
4.14.1 矩形碰撞
public boolean isCollsion(Rect rectA, Rect rectB) {
// left
// A B
if (rectA.left < rectB.left && rectA.right < rectB.left) {
return false;
}
// top
// A
// B
if (rectA.bottom < rectB.top && rectA.bottom < rectB.bottom) {
return false;
}
// right
// B A
if (rectA.left > rectB.left && rectA.left > rectB.right) {
return false;
}
// bottom
// B
// A
if (rectA.top > rectB.bottom && rectA.bottom > rectB.bottom) {
return false;
}
return true;
}
ラベル:
Android游戏编程之从零开始
2019年3月24日日曜日
Android游戏编程之从零开始、p173
Android游戏编程之从零开始、p173
自定义动画
动态位图
private void T1set() {
btax = bitmapB.getWidth() - this.getWidth();
btay = -bitmapB.getHeight() + this.getHeight();
}
private void T1(Canvas canvas) {
btax -= 10;
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmapB, -btax, btay, paint);
}
private void mDraw() {
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
T1(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void run() {
while (thflag) {
long s = System.currentTimeMillis();
mDraw();
long e = System.currentTimeMillis();
if (e - s < 50) {
try {
Thread.sleep(50 - (e - s));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
自定义动画
动态位图
private void T1set() {
btax = bitmapB.getWidth() - this.getWidth();
btay = -bitmapB.getHeight() + this.getHeight();
}
private void T1(Canvas canvas) {
btax -= 10;
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmapB, -btax, btay, paint);
}
private void mDraw() {
try {
canvas = surfaceHolder.lockCanvas();
if (canvas != null) {
T1(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void run() {
while (thflag) {
long s = System.currentTimeMillis();
mDraw();
long e = System.currentTimeMillis();
if (e - s < 50) {
try {
Thread.sleep(50 - (e - s));
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
ラベル:
Android游戏编程之从零开始
Android游戏编程之从零开始 P166
Android游戏编程之从零开始
②第二种是利用Regin来对画布设置可视区域。
canvas.clipRegion(region) 过时。
canvas.clipPath(region.getBoundaryPath()) 利用
private void T3(Canvas canvas) {
canvas.save();
canvas.drawColor(Color.WHITE);
Region region = new Region();
region.op(new Rect(200, 200, 1000, 1000), Region.Op.UNION);
region.op(new Rect(400, 300, 900, 900), Region.Op.REVERSE_DIFFERENCE);
region.op(new Rect(500, 200, 800, 800), Region.Op.XOR);
canvas.clipPath(region.getBoundaryPath());
canvas.drawBitmap(bitmapC, 0, 0, paint);
canvas.restore();
}
②第二种是利用Regin来对画布设置可视区域。
canvas.clipRegion(region) 过时。
canvas.clipPath(region.getBoundaryPath()) 利用
private void T3(Canvas canvas) {
canvas.save();
canvas.drawColor(Color.WHITE);
Region region = new Region();
region.op(new Rect(200, 200, 1000, 1000), Region.Op.UNION);
region.op(new Rect(400, 300, 900, 900), Region.Op.REVERSE_DIFFERENCE);
region.op(new Rect(500, 200, 800, 800), Region.Op.XOR);
canvas.clipPath(region.getBoundaryPath());
canvas.drawBitmap(bitmapC, 0, 0, paint);
canvas.restore();
}
adb devices 查询设备
查询设备
$ adb devices
将文件复制到设备/从设备复制文件
$ adb pull remote local
$ adb push local remote
进行屏幕截图
$ adb shell screencap /sdcard/screen.png
录制视频
$ adb shell screenrecord /sdcard/demo.mp4
$ adb devices
将文件复制到设备/从设备复制文件
$ adb pull remote local
$ adb push local remote
进行屏幕截图
$ adb shell screencap /sdcard/screen.png
录制视频
$ adb shell screenrecord /sdcard/demo.mp4
登録:
投稿 (Atom)