218 lines
7.1 KiB
Plaintext
218 lines
7.1 KiB
Plaintext
package mangavalk.canvasfirsttry;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.res.Resources;
|
|
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.Paint.Style;
|
|
import android.graphics.RectF;
|
|
import android.util.DisplayMetrics;
|
|
import android.util.TypedValue;
|
|
import android.view.Display;
|
|
import android.view.View;
|
|
|
|
public class CanvasView extends View {
|
|
Resources r ;
|
|
float dpHeight;
|
|
float dpWidth;
|
|
float Height;
|
|
float Width;
|
|
float moveX1,moveX2;
|
|
float moveY1,moveY2;
|
|
|
|
long oldTime;
|
|
|
|
public CanvasView(Context context) {
|
|
super(context);
|
|
|
|
super.setBackgroundColor(Color.WHITE);
|
|
oldTime = System.currentTimeMillis();
|
|
|
|
this.postInvalidate();
|
|
}
|
|
|
|
public void SetActivity(Activity activity)
|
|
{
|
|
Display display = activity.getWindowManager().getDefaultDisplay();
|
|
DisplayMetrics outMetrics = new DisplayMetrics ();
|
|
display.getMetrics(outMetrics);
|
|
|
|
r = getResources();
|
|
float density = getResources().getDisplayMetrics().density;
|
|
dpHeight = outMetrics.heightPixels / density;
|
|
dpWidth = outMetrics.widthPixels / density;
|
|
Height = outMetrics.heightPixels;
|
|
Width = outMetrics.widthPixels;
|
|
|
|
moveY1 = 50;
|
|
moveY2 = 50;
|
|
}
|
|
|
|
int count = 0;
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
count++;
|
|
|
|
long newTime = System.currentTimeMillis();
|
|
|
|
//drawText(canvas, count);
|
|
DrawFps(canvas, newTime);
|
|
drawPeddleRight(canvas, moveY1);
|
|
drawPeddleLeft(canvas, moveY2);
|
|
|
|
oldTime = newTime;
|
|
|
|
//this.postInvalidateDelayed( 100 );
|
|
this.postInvalidate();
|
|
}
|
|
|
|
private void drawRoundedRectangle(Canvas canvas) {
|
|
// TODO Auto-generated method stub
|
|
Paint paint = new Paint();
|
|
paint.setAntiAlias(true);
|
|
paint.setColor(Color.MAGENTA);
|
|
paint.setStyle(Style.FILL);
|
|
int xPosition = 10;
|
|
int yPosition = 350;
|
|
int rectangleWidth = 100;
|
|
int rectangleHeight = 50;
|
|
RectF rect=new RectF(xPosition, yPosition, xPosition + rectangleWidth,
|
|
yPosition + rectangleHeight);
|
|
canvas.drawRoundRect(rect, 10,10, paint);
|
|
paint.setStyle(Style.STROKE);
|
|
xPosition = 150;
|
|
rect=new RectF(xPosition, yPosition, xPosition + rectangleWidth,
|
|
yPosition + rectangleHeight);
|
|
canvas.drawRoundRect(rect, 10,10, paint);
|
|
|
|
}
|
|
|
|
private void drawRotatedImage(Canvas canvas) {
|
|
// TODO Auto-generated method stub
|
|
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
|
|
R.drawable.ic_launcher);
|
|
bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
|
|
Matrix matrix = new Matrix();
|
|
matrix.postRotate(60);// angle to rotate in clockwise direction
|
|
matrix.postTranslate(200, 180);
|
|
|
|
canvas.drawBitmap(bitmap, matrix, null);
|
|
}
|
|
|
|
private void DrawFps(Canvas canvas, long newTime)
|
|
{
|
|
Paint paint = new Paint();
|
|
paint.setColor(Color.BLACK);
|
|
paint.setTextSize(30);
|
|
String text = "FPS: " + (1000/(newTime-oldTime));
|
|
canvas.drawText(text, GetRealPixelSize(dpWidth, 5), GetRealPixelSize(dpHeight, 5), paint);
|
|
}
|
|
|
|
float map(float x, float in_min, float in_max, float out_min, float out_max)
|
|
{
|
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
}
|
|
|
|
private void drawPeddleRight(Canvas canvas, float i) {
|
|
Paint paint = new Paint();
|
|
paint.setAntiAlias(true);
|
|
paint.setColor(Color.BLACK);
|
|
paint.setStyle(Style.FILL);
|
|
|
|
float rectangleWidth = 2;
|
|
float rectangleHeight = 20;
|
|
|
|
float xPosition = 97;
|
|
float yPosition = i;
|
|
yPosition = yPosition <= (100-(rectangleHeight / 2))?yPosition:(100-(rectangleHeight / 2));
|
|
yPosition = yPosition >= (rectangleHeight / 2)?yPosition:(rectangleHeight / 2);
|
|
|
|
canvas.drawRect(
|
|
GetRealPixelSize(dpWidth, xPosition),
|
|
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight / 2)),
|
|
GetRealPixelSize(dpWidth, xPosition + (rectangleWidth)),
|
|
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight / 2)), paint);
|
|
}
|
|
|
|
private void drawPeddleLeft(Canvas canvas, float i) {
|
|
Paint paint = new Paint();
|
|
paint.setAntiAlias(true);
|
|
paint.setColor(Color.BLACK);
|
|
paint.setStyle(Style.FILL);
|
|
|
|
float rectangleWidth = 2;
|
|
float rectangleHeight = 20;
|
|
|
|
float xPosition = 3;
|
|
float yPosition = i;
|
|
yPosition = yPosition <= (100-(rectangleHeight / 2))?yPosition:(100-(rectangleHeight / 2));
|
|
yPosition = yPosition >= (rectangleHeight / 2)?yPosition:(rectangleHeight / 2);
|
|
|
|
canvas.drawRect(
|
|
GetRealPixelSize(dpWidth, xPosition - (rectangleWidth)),
|
|
GetRealPixelSize(dpHeight, yPosition - (rectangleHeight / 2)),
|
|
GetRealPixelSize(dpWidth, xPosition),
|
|
GetRealPixelSize(dpHeight, yPosition + (rectangleHeight / 2)), paint);
|
|
}
|
|
|
|
private float GetRealPixelSize(float dpWH, float DIP)
|
|
{
|
|
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (dpWH * ( DIP / 100 )), r.getDisplayMetrics());
|
|
}
|
|
|
|
private void drawImage(Canvas canvas) {
|
|
// TODO Auto-generated method stub
|
|
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
|
|
R.drawable.ic_launcher);
|
|
bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
|
|
canvas.drawBitmap(bitmap, 10, 200, null);
|
|
}
|
|
|
|
private void drawRectangle(Canvas canvas) {
|
|
// TODO Auto-generated method stub
|
|
Paint paint = new Paint();
|
|
paint.setAntiAlias(true);
|
|
paint.setColor(Color.RED);
|
|
paint.setStyle(Style.FILL);
|
|
int xPosition = 10;
|
|
int yPosition = 150;
|
|
int rectangleWidth = 100;
|
|
int rectangleHeight = 50;
|
|
canvas.drawRect(xPosition, yPosition, xPosition + rectangleWidth,
|
|
yPosition + rectangleHeight, paint);
|
|
paint.setStyle(Style.STROKE);
|
|
xPosition = 150;
|
|
canvas.drawRect(xPosition, yPosition, xPosition + rectangleWidth,
|
|
yPosition + rectangleHeight, paint);
|
|
}
|
|
|
|
private void drawCircle(Canvas canvas) {
|
|
// TODO Auto-generated method stub
|
|
Paint paint = new Paint();
|
|
paint.setAntiAlias(true);
|
|
paint.setColor(Color.BLUE);
|
|
paint.setStyle(Style.FILL);
|
|
canvas.drawCircle(50, 60, 50, paint);
|
|
paint.setStyle(Style.STROKE);
|
|
canvas.drawCircle(200, 60, 50, paint);
|
|
}
|
|
|
|
public void sendMove(float x, float y) {
|
|
if (x / Width >= 0.6f)
|
|
{
|
|
moveX1 = (x / Width) * 100;
|
|
moveY1 = (y / Height) * 100;
|
|
}
|
|
else if (x / Width <= 0.4f)
|
|
{
|
|
moveX2 = (x / Width) * 100;
|
|
moveY2 = (y / Height) * 100;
|
|
}
|
|
}
|
|
} |