1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
package com.example.surfaceviewexample;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SurfaceViewActivity extends Activity implements Runnable {
private Button swapBtn;
private SurfaceView surface;
private SurfaceHolder holder;
private boolean locker=true;
private Thread thread;
private int radiusBlack, radiusWhite;
private boolean left = true;
//physics
private static final int baseRadius = 10;
private static final int maxRadius = 50;
private static final int baseSpeed = 1;
private int speed = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surface_view);
swapBtn = (Button) findViewById(R.id.buttonswap);
surface = (SurfaceView) findViewById(R.id.mysurface);
holder = surface.getHolder();
thread = new Thread(this);
thread.start();
swapBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/** SWAP ANIMATION LEFT OR RIGHT CIRCLE*/
left = !left;
}
});
}
@Override
public void run() {
// TODO Auto-generated method stub
while(locker){
//checks if the lockCanvas() method will be success,and if not, will check this statement again
if(!holder.getSurface().isValid()){
continue;
}
/** Start editing pixels in this surface.*/
Canvas canvas = holder.lockCanvas();
//ALL PAINT-JOB MAKE IN draw(canvas); method.
draw(canvas);
// End of painting to canvas. system will paint with this canvas,to the surface.
holder.unlockCanvasAndPost(canvas);
}
}
/**This method deals with paint-works. Also will paint something in background*/
private void draw(Canvas canvas) {
// paint a background color
canvas.drawColor(android.R.color.holo_blue_bright);
// paint a rectangular shape that fill the surface.
int border = 20;
RectF r = new RectF(border, border, canvas.getWidth()-20, canvas.getHeight()-20);
Paint paint = new Paint();
paint.setARGB(200, 135, 135, 135); //paint color GRAY+SEMY TRANSPARENT
canvas.drawRect(r , paint );
/*
* i want to paint to circles, black and white. one of circles will
* bounce, tile the button 'swap' pressed and then other circle begin bouncing.
*/
calculateRadiuses();
//paint left circle(black)
paint.setColor(getResources().getColor(android.R.color.black));
canvas.drawCircle(canvas.getWidth()/4, canvas.getHeight()/2, radiusBlack, paint);
//paint right circle(white)
paint.setColor(getResources().getColor(android.R.color.white));
canvas.drawCircle(canvas.getWidth()/4*3, canvas.getHeight()/2, radiusWhite, paint);
}
private void calculateRadiuses() {
// TODO Auto-generated method stub
if(left){
updateSpeed(radiusBlack);
radiusBlack += speed;
radiusWhite = baseRadius;
}
else{
updateSpeed(radiusWhite);
radiusWhite += speed;
radiusBlack = baseRadius;
}
}
/**Change speed according to current radius size.
* if, radius is bigger than maxRad the speed became negative otherwise
* if radius is smaller then baseRad speed will positive.
* @param radius
*/
private void updateSpeed(int radius) {
// TODO Auto-generated method stub
if(radius>=maxRadius){
speed = -baseSpeed;
}
else if (radius<=baseRadius){
speed = baseSpeed;
}
}
@Override
protected void onPause() {
super.onPause();
pause();
}
private void pause() {
//CLOSE LOCKER FOR run();
locker = false;
while(true){
try {
//WAIT UNTIL THREAD DIE, THEN EXIT WHILE LOOP AND RELEASE a thread
thread.join();
} catch (InterruptedException e) {e.printStackTrace();
}
break;
}
thread = null;
}
@Override
protected void onResume() {
super.onResume();
resume();
}
private void resume() {
//RESTART THREAD AND OPEN LOCKER FOR run();
locker = true;
}
} |
Partager