Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bluehomestudio.luckywheel.LuckyWheel;
import com.bluehomestudio.luckywheel.OnLuckyWheelReachTheTarget;
import com.bluehomestudio.luckywheel.WheelItem;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private LuckyWheel lw;
List<WheelItem> wheelItems ;
List<WheelItem> wheelItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -35,34 +35,33 @@ protected void onCreate(Bundle savedInstanceState) {

lw.setLuckyWheelReachTheTarget(new OnLuckyWheelReachTheTarget() {
@Override
public void onReachTarget() {
Toast.makeText(MainActivity.this, "Target Reached", Toast.LENGTH_LONG).show();
public void onReachTarget(WheelItem item) {
Toast.makeText(MainActivity.this, "Target Reached " + item.text, Toast.LENGTH_LONG).show();
}
});

Button start = findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lw.rotateWheelTo(1);
lw.rotateWheelTo(5);
}
});

}

private void generateWheelItems() {
wheelItems = new ArrayList<>();
wheelItems.add(new WheelItem(Color.parseColor("#fc6c6c"), BitmapFactory.decodeResource(getResources(),
R.drawable.chat) , "100 $"));
wheelItems.add(new WheelItem(Color.parseColor("#00E6FF"), BitmapFactory.decodeResource(getResources(),
R.drawable.coupon) , "0 $"));
wheelItems.add(new WheelItem(Color.parseColor("#F00E6F"), BitmapFactory.decodeResource(getResources(),
wheelItems.add(new WheelItem(1, Color.parseColor("#fc6c6c"), BitmapFactory.decodeResource(getResources(),
R.drawable.chat), "100 $"));
wheelItems.add(new WheelItem(2, Color.parseColor("#00E6FF"), "0 $"));
wheelItems.add(new WheelItem(3, Color.parseColor("#F00E6F"), BitmapFactory.decodeResource(getResources(),
R.drawable.ice_cream), "30 $"));
wheelItems.add(new WheelItem(Color.parseColor("#00E6FF"), BitmapFactory.decodeResource(getResources(),
wheelItems.add(new WheelItem(4, Color.parseColor("#00E6FF"), BitmapFactory.decodeResource(getResources(),
R.drawable.lemonade), "6000 $"));
wheelItems.add(new WheelItem(Color.parseColor("#fc6c6c"), BitmapFactory.decodeResource(getResources(),
wheelItems.add(new WheelItem(5, Color.parseColor("#fc6c6c"), BitmapFactory.decodeResource(getResources(),
R.drawable.orange), "9 $"));
wheelItems.add(new WheelItem(Color.parseColor("#00E6FF"), BitmapFactory.decodeResource(getResources(),
wheelItems.add(new WheelItem(6, Color.parseColor("#00E6FF"), BitmapFactory.decodeResource(getResources(),
R.drawable.shop), "20 $"));
}
}
20 changes: 10 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:LuckyWheel="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bluehomestudio.luckywheeldemo.MainActivity">

<com.bluehomestudio.luckywheel.LuckyWheel
android:id="@+id/lwv"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
LuckyWheel:background_color="@color/colorPrimary" />

<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="#00E6FF"
android:textColor="@android:color/white"
android:text="@string/start"/>
android:text="@string/start"
android:textColor="@android:color/white" />

<com.bluehomestudio.luckywheel.LuckyWheel
android:id="@+id/lwv"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
LuckyWheel:background_color="@color/colorPrimary"
LuckyWheel:text_size="16dp" />

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.List;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import androidx.core.content.res.ResourcesCompat;

/**
* Created by mohamed on 22/04/17.
Expand Down Expand Up @@ -60,9 +62,17 @@ public void applyAttribute(AttributeSet attrs) {
int backgroundColor = typedArray.getColor(R.styleable.LuckyWheel_background_color, Color.GREEN);
int arrowImage = typedArray.getResourceId(R.styleable.LuckyWheel_arrow_image, R.drawable.arrow);
int imagePadding = typedArray.getDimensionPixelSize(R.styleable.LuckyWheel_image_padding , 0);
wheelView.setWheelBackgoundWheel(backgroundColor);
int textSize = typedArray.getDimensionPixelSize(R.styleable.LuckyWheel_text_size, 30);
int fontFamilyId = typedArray.getResourceId(R.styleable.LuckyWheel_android_fontFamily, 0);
int archStyle = typedArray.getInt(R.styleable.LuckyWheel_style, 0);
wheelView.setWheelBackgroundWheel(backgroundColor);
wheelView.setItemsImagePadding(imagePadding);
arrow.setImageResource(arrowImage);
wheelView.setTextSize(textSize);
wheelView.setStyle(Paint.Style.values()[archStyle]);
if (fontFamilyId > 0) {
wheelView.setTypeface(ResourcesCompat.getFont(getContext(), fontFamilyId));
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/

public interface OnLuckyWheelReachTheTarget {
void onReachTarget();
void onReachTarget(WheelItem item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

public class WheelItem {

public int id;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason to add a new id variable to the WheelItem
Check getAngleOfIndexTarget the calculations already depend on the index of the WheelItem so it's risky just to let the user set the id himself

public int color;
public Bitmap bitmap;
public String text;

public WheelItem(int color, Bitmap bitmap) {
public WheelItem(int id, int color, Bitmap bitmap) {
this.id = id;
this.color = color;
this.bitmap = bitmap;
}

public WheelItem(int color, Bitmap bitmap, String text) {
public WheelItem(int id, int color, String text) {
this.id = id;
this.color = color;
this.text = text;
}

public WheelItem(int id, int color, Bitmap bitmap, String text) {
this.id = id;
this.color = color;
this.bitmap = bitmap;
this.text = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.IntRange;
import androidx.annotation.Nullable;

/**
* Created by mohamed on 22/04/17.
*/
Expand All @@ -27,10 +29,14 @@ final class WheelView extends View {
private RectF range = new RectF();
private Paint archPaint, textPaint;
private int padding, radius, center, mWheelBackground, mImagePadding;
private List<WheelItem> mWheelItems;
private List<WheelItem> mWheelItems = new ArrayList<>();
private OnLuckyWheelReachTheTarget mOnLuckyWheelReachTheTarget;
private OnRotationListener onRotationListener;

private float textSize = 30;
private Typeface typeface;
private Paint.Style archStyle = Paint.Style.FILL;

public WheelView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
Expand All @@ -44,12 +50,18 @@ private void initComponents() {
archPaint = new Paint();
archPaint.setAntiAlias(true);
archPaint.setDither(true);
archPaint.setStyle(archStyle);
//text paint object
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setAntiAlias(true);
textPaint.setDither(true);
textPaint.setTextSize(30);
textPaint.setTextSize(textSize);

if (typeface != null) {
textPaint.setTypeface(typeface);
}

//rect rang of the arc
range = new RectF(padding, padding, padding + radius, padding + radius);
}
Expand All @@ -59,16 +71,16 @@ private void initComponents() {
*
* @return Number of angle
*/
private float getAngleOfIndexTarget(int target) {
return (360 / mWheelItems.size()) * target;
private float getAngleOfIndexTarget(@IntRange(from = 1) int target) {
return (360f / mWheelItems.size()) * target;
}

/**
* Function to set wheel background
*
* @param wheelBackground Wheel background color
*/
public void setWheelBackgoundWheel(int wheelBackground) {
public void setWheelBackgroundWheel(int wheelBackground) {
mWheelBackground = wheelBackground;
invalidate();
}
Expand Down Expand Up @@ -130,11 +142,10 @@ private void drawImage(Canvas canvas, float tempAngle, Bitmap bitmap) {
float px = rect.exactCenterX();
float py = rect.exactCenterY();
Matrix matrix = new Matrix();
matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2);
matrix.postTranslate(-bitmap.getWidth() / 2f, -bitmap.getHeight() / 2f);
matrix.postRotate(tempAngle + 120);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG ));
Log.d("sadsdsddssd" , bitmap.getWidth() + " : "+bitmap.getHeight());
canvas.drawBitmap(bitmap, matrix, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG));
matrix.reset();
}

Expand All @@ -161,9 +172,9 @@ private void drawText(Canvas canvas, float tempAngle, float sweepAngle, String t
*
* @param target target number
*/
public void rotateWheelToTarget(int target) {
public void rotateWheelToTarget(final int target) {

float wheelItemCenter = 270 - getAngleOfIndexTarget(target) + (360 / mWheelItems.size()) / 2;
float wheelItemCenter = 270 - getAngleOfIndexTarget(target) + (360f / mWheelItems.size()) / 2;
int DEFAULT_ROTATION_TIME = 9000;
animate().setInterpolator(new DecelerateInterpolator())
.setDuration(DEFAULT_ROTATION_TIME)
Expand All @@ -177,7 +188,7 @@ public void onAnimationStart(Animator animation) {
@Override
public void onAnimationEnd(Animator animation) {
if (mOnLuckyWheelReachTheTarget != null) {
mOnLuckyWheelReachTheTarget.onReachTarget();
mOnLuckyWheelReachTheTarget.onReachTarget(mWheelItems.get(target - 1));
}
if (onRotationListener != null) {
onRotationListener.onFinishRotation();
Expand Down Expand Up @@ -229,6 +240,21 @@ public void onAnimationRepeat(Animator animation) {
});
}

public void setTypeface(Typeface typeface) {
this.typeface = typeface;
invalidate();
}

public void setTextSize(float textSize) {
this.textSize = textSize;
invalidate();
}

public void setStyle(Paint.Style style) {
this.archStyle = style;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Expand All @@ -237,13 +263,19 @@ protected void onDraw(Canvas canvas) {
initComponents();

float tempAngle = 0;
float sweepAngle = 360 / mWheelItems.size();
float sweepAngle = 360;
if (!mWheelItems.isEmpty()) {
sweepAngle = 360f / mWheelItems.size();
}

for (int i = 0; i < mWheelItems.size(); i++) {
archPaint.setColor(mWheelItems.get(i).color);
WheelItem item = mWheelItems.get(i);
archPaint.setColor(item.color);
canvas.drawArc(range, tempAngle, sweepAngle, true, archPaint);
drawImage(canvas, tempAngle, mWheelItems.get(i).bitmap);
drawText(canvas, tempAngle, sweepAngle, mWheelItems.get(i).text == null ? "" : mWheelItems.get(i).text);
if (item.bitmap != null) {
drawImage(canvas, tempAngle, item.bitmap);
}
drawText(canvas, tempAngle, sweepAngle, item.text == null ? "" : mWheelItems.get(i).text);
tempAngle += sweepAngle;
}

Expand Down
9 changes: 8 additions & 1 deletion luckywheel/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
<resources>

<declare-styleable name="LuckyWheel">
<attr name="style" format="enum">
<enum name="fill" value="0"/>
<enum name="stroke" value="1"/>
<enum name="fill_and_stroke" value="2"/>
</attr>

<attr name="background_color" format="color"/>
<attr name="arrow_image" format="reference"/>
<attr name="square_layout" format="boolean"/>
<attr name="image_padding" format="dimension"/>
<attr name="text_size" format="dimension"/>
<attr name="android:fontFamily" />
</declare-styleable>

</resources>
</resources>