在自定义的SurfaceView 上打开 DialogFragment
myDialogFragment.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), "text");
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean show = true;
if (show) {
AboutDialog aboutDialog = new AboutDialog(getContext());
aboutDialog.show();
Toast.makeText(getContext(), "xxxxxxxxxx", Toast.LENGTH_SHORT).show();
} else {
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.item("99");
myDialogFragment.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), "text");
}
return true;
}
2019年5月22日水曜日
2019年4月13日土曜日
admob 設定
admob 設定
public class MainActivity extends AppCompatActivity {
private String T = "### MainActivity";
private MySurfaceView3 mySurfaceView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySurfaceView = new MySurfaceView3(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
addContentView(mySurfaceView, rl);
Google_admob();
View view = findViewById(R.id.activity_main);
view.bringToFront();
}
protected void Google_admob() {
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
ViewGroup viewGroup = findViewById(R.id.activity_main);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adView.setLayoutParams(layoutParams);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice("7026FA2EC1DC7E60FBEA02C64D33BD8B");
builder.addTestDevice("53185CF5BFA5B2121DF7FA86E7064C22");
AdRequest adRequest = builder.build();
adView.loadAd(adRequest);
viewGroup.addView(adView);
adView.setAdListener(new AdListener() {
@Override public void onAdLoaded() {
super.onAdLoaded();
Log.i(T, "onAdLoaded");
}
@Override public void onAdOpened() {
super.onAdOpened();
Log.i(T, "onAdOpened");
}
@Override public void onAdLeftApplication() {
super.onAdLeftApplication();
Log.i(T, "onAdLeftApplication");
}
@Override public void onAdClosed() {
super.onAdClosed();
Log.i(T, "onAdClosed");
}
});
}
}
//
I / ### MainActivity: onAdLoaded
I / ### MainActivity: onAdOpened
I / ### MainActivity: onAdLeftApplication
I / ### MainActivity: onAdClosed
I / ### MainActivity: onAdLoaded
Java 練習
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,
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年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游戏编程之从零开始
登録:
投稿 (Atom)