button, background set bitmap

This commit is contained in:
edgar 2015-11-01 21:55:58 -08:00
parent 776c514ebb
commit 266caaadeb
4 changed files with 118 additions and 22 deletions

View file

@ -19,6 +19,10 @@ public class MainActivity extends AppCompatActivity implements JoyStick.JoyStick
((JoyStick)findViewById(R.id.joy1)).setPadColor(Color.parseColor("#55ffffff")); ((JoyStick)findViewById(R.id.joy1)).setPadColor(Color.parseColor("#55ffffff"));
((JoyStick)findViewById(R.id.joy1)).setButtonColor(Color.parseColor("#55ff0000")); ((JoyStick)findViewById(R.id.joy1)).setButtonColor(Color.parseColor("#55ff0000"));
((JoyStick)findViewById(R.id.joy2)).setListener(this); ((JoyStick)findViewById(R.id.joy2)).setListener(this);
((JoyStick)findViewById(R.id.joy2)).enableStayPut(true);
((JoyStick)findViewById(R.id.joy2)).setButtonSize(50);
((JoyStick)findViewById(R.id.joy2)).setButtonDrawable(R.drawable.droid);
((JoyStick)findViewById(R.id.joy2)).setButtonAlpha(100);
} }
@Override @Override

View file

@ -2,7 +2,6 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"> android:orientation="vertical">
<com.erz.joystick.GameView <com.erz.joystick.GameView
@ -20,8 +19,6 @@
android:id="@+id/joy2" android:id="@+id/joy2"
android:layout_width="200dp" android:layout_width="200dp"
android:layout_height="200dp" android:layout_height="200dp"
android:layout_gravity="bottom|right" android:layout_gravity="bottom|right"/>
app:padColor="#55ffffff"
app:buttonColor="#55ff0000"/>
</FrameLayout> </FrameLayout>

View file

@ -18,9 +18,12 @@ package com.erz.joysticklibrary;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@ -30,6 +33,8 @@ import android.view.View;
*/ */
public class JoyStick extends View { public class JoyStick extends View {
JoyStickListener listener;
Paint paint;
float width; float width;
float height; float height;
float centerX; float centerX;
@ -39,12 +44,33 @@ public class JoyStick extends View {
float posY; float posY;
float radius; float radius;
float buttonRadius; float buttonRadius;
double power; double power = -1;
double angle; double angle = -1;
RectF temp;
//Background Color
int padColor; int padColor;
//Stick Color
int buttonColor; int buttonColor;
Paint paint;
JoyStickListener listener; //Keeps joystick in last position
boolean stayPut;
//Button Size percentage of the minimum(width, height)
int percentage = 25;
//Background Bitmap
Bitmap padBGBitmap = null;
//Button Bitmap
Bitmap buttonBitmap = null;
//Background Alpha
int padAlpha = 255;
//Button Alpha
int buttonAlpha = 255;
public interface JoyStickListener { public interface JoyStickListener {
void onMove(JoyStick joyStick, double angle, double power); void onMove(JoyStick joyStick, double angle, double power);
@ -65,14 +91,33 @@ public class JoyStick extends View {
paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true); paint.setAntiAlias(true);
padColor = Color.BLACK; temp = new RectF();
padColor = Color.WHITE;
buttonColor = Color.RED; buttonColor = Color.RED;
if (attrs != null) { if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.JoyStick); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.JoyStick);
if (typedArray != null) { if (typedArray != null) {
padColor = typedArray.getColor(R.styleable.JoyStick_padColor, Color.BLACK); padColor = typedArray.getColor(R.styleable.JoyStick_padColor, Color.WHITE);
buttonColor = typedArray.getColor(R.styleable.JoyStick_buttonColor, Color.RED); buttonColor = typedArray.getColor(R.styleable.JoyStick_buttonColor, Color.RED);
stayPut = typedArray.getBoolean(R.styleable.JoyStick_stayPut, false);
percentage = typedArray.getInt(R.styleable.JoyStick_percentage, 25);
if (percentage > 50) percentage = 50;
if (percentage < 25) percentage = 25;
int padResId = typedArray.getResourceId(R.styleable.JoyStick_backgroundDrawable, -1);
int buttonResId = typedArray.getResourceId(R.styleable.JoyStick_buttonDrawable, -1);
if (padResId > 0) {
padBGBitmap = BitmapFactory.decodeResource(getResources(), padResId);
}
if (buttonResId > 0) {
buttonBitmap = BitmapFactory.decodeResource(getResources(), buttonResId);
}
padAlpha = typedArray.getInt(R.styleable.JoyStick_backgroundAlpha, 255);
buttonAlpha = typedArray.getInt(R.styleable.JoyStick_buttonAlpha, 255);
typedArray.recycle(); typedArray.recycle();
} }
} }
@ -88,7 +133,7 @@ public class JoyStick extends View {
min = Math.min(width, height); min = Math.min(width, height);
posX = centerX; posX = centerX;
posY = centerY; posY = centerY;
buttonRadius = (min / 2 * 0.25f); buttonRadius = (min / 2f * (percentage/100f));
radius = (min / 2 * 0.75f); radius = (min / 2 * 0.75f);
} }
@ -96,10 +141,22 @@ public class JoyStick extends View {
public void draw(Canvas canvas) { public void draw(Canvas canvas) {
super.draw(canvas); super.draw(canvas);
if (canvas == null) return; if (canvas == null) return;
if (padBGBitmap == null) {
paint.setColor(padColor); paint.setColor(padColor);
canvas.drawCircle(centerX, centerY, radius, paint); canvas.drawCircle(centerX, centerY, radius, paint);
} else {
paint.setAlpha(padAlpha);
temp.set(posX - radius, posY - radius, posX + radius, posY + radius);
canvas.drawBitmap(padBGBitmap, null, temp, paint);
}
if (buttonBitmap == null) {
paint.setColor(buttonColor); paint.setColor(buttonColor);
canvas.drawCircle(posX, posY, buttonRadius, paint); canvas.drawCircle(posX, posY, buttonRadius, paint);
} else {
paint.setAlpha(buttonAlpha);
temp.set(posX - buttonRadius, posY - buttonRadius, posX + buttonRadius, posY + buttonRadius);
canvas.drawBitmap(buttonBitmap, null, temp, paint);
}
} }
@Override @Override
@ -126,11 +183,13 @@ public class JoyStick extends View {
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
if (!stayPut) {
posX = centerX; posX = centerX;
posY = centerY; posY = centerY;
angle = 0; angle = 0;
power = 0; power = 0;
invalidate(); invalidate();
}
break; break;
} }
@ -142,12 +201,10 @@ public class JoyStick extends View {
public void setPadColor(int padColor) { public void setPadColor(int padColor) {
this.padColor = padColor; this.padColor = padColor;
invalidate();
} }
public void setButtonColor(int buttonColor) { public void setButtonColor(int buttonColor) {
this.buttonColor = buttonColor; this.buttonColor = buttonColor;
invalidate();
} }
public void setListener(JoyStickListener listener) { public void setListener(JoyStickListener listener) {
@ -165,4 +222,36 @@ public class JoyStick extends View {
public double getAngleDegrees() { public double getAngleDegrees() {
return Math.toDegrees(angle); return Math.toDegrees(angle);
} }
public void enableStayPut(boolean enable) {
this.stayPut = enable;
}
//size of button is a percentage of the minimum(width, height)
//percentage must be between 25 - 50
public void setButtonSize(int p) {
percentage = p;
if (percentage > 50) percentage = 50;
if (percentage < 25) percentage = 25;
}
public void setPadBackground(int resId) {
this.padBGBitmap = BitmapFactory.decodeResource(getResources(), resId);
}
public void setButtonDrawable(int resId) {
this.buttonBitmap = BitmapFactory.decodeResource(getResources(), resId);
}
public void setBackgroundAlpha(int alpha) {
padAlpha = alpha;
if (padAlpha > 255) padAlpha = 255;
if (padAlpha < 0) padAlpha = 0;
}
public void setButtonAlpha(int alpha) {
buttonAlpha = alpha;
if (buttonAlpha > 255) buttonAlpha = 255;
if (buttonAlpha < 0) buttonAlpha = 0;
}
} }

View file

@ -3,5 +3,11 @@
<declare-styleable name="JoyStick"> <declare-styleable name="JoyStick">
<attr name="padColor" format="color" /> <attr name="padColor" format="color" />
<attr name="buttonColor" format="color" /> <attr name="buttonColor" format="color" />
<attr name="stayPut" format="boolean" />
<attr name="percentage" format="integer" />
<attr name="backgroundDrawable" format="reference" />
<attr name="buttonDrawable" format="reference" />
<attr name="backgroundAlpha" format="integer" />
<attr name="buttonAlpha" format="integer" />
</declare-styleable> </declare-styleable>
</resources> </resources>