2018. 11. 6. 14:36ㆍAndroid
ㅇ RouletteActivity.java
public class RouletteActivity extends AppCompatActivity {
private ImageView mImageView;
private Button rotate;
private Bitmap mBitmap;
private float angle = 0.0f; // 초기 각도
private final int IMG_DP = 300; // 이미지 DP
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roulette);
mImageView = findViewById(R.id.wheel);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roulette);
mImageView.setImageBitmap(onResizeImage(mBitmap));
rotate = findViewById(R.id.rotate);
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onWheelImage();
}
});
}
/**
* from DP to Pixel
* @param dp
* @param context
* @return
*/
private static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
/**
* image resizing
* @param bitmap
* @return
*/
private Bitmap onResizeImage(Bitmap bitmap){
int height = bitmap.getHeight();
int width = bitmap.getWidth();
Float size = convertDpToPixel(IMG_DP, this);
Bitmap resized = null;
while(height > size.intValue()){
resized = Bitmap.createScaledBitmap(bitmap, (width*size.intValue())/height, size.intValue(), true);
height = resized.getHeight();
width = resized.getWidth();
}
return resized;
}
private int getRandom(int max){
return (int) (Math.random()*max);
}
private void onWheelImage(){
runOnUiThread(new Runnable() {
@Override
public void run() {
int random = getRandom(360);
float fromAngle = random+720+angle; // 회전수 제어(랜덤(0~360)+720도+회전각)
// 로테이션 애니메이션 초기화
// 시작각, 종료각, 자기 원을 그리며 회전 옵
RotateAnimation animation = new RotateAnimation(angle, fromAngle
, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
getTargetEvent(fromAngle);
// 초기 시작각도 업데이트
angle = fromAngle;
animation.setDuration(2000); // 지속시간이 길수록 느려짐
animation.setFillEnabled(true); // 애니메이션 종료된 후 상태 고정 옵션
animation.setFillAfter(true);
mImageView.startAnimation(animation);
}
});
}
private void getTargetEvent(float value){
if((value>=1051 && value<=1080) || (value>=720 && value<=750)){
Toast.makeText(getApplicationContext(), "콤보 할인 !!!!", Toast.LENGTH_SHORT).show();
}else if(value>=991 && value<=1050){
Toast.makeText(getApplicationContext(), "팝콘 교환 !!!!", Toast.LENGTH_SHORT).show();
}else if(value>=931 && value<=990){
Toast.makeText(getApplicationContext(), "영화 할인 !!!!", Toast.LENGTH_SHORT).show();
}else if(value>=871 && value<=930){
Toast.makeText(getApplicationContext(), "꽝 !!!!", Toast.LENGTH_SHORT).show();
}else if(value>=811 && value<=870){
Toast.makeText(getApplicationContext(), "음료수 !!!!", Toast.LENGTH_SHORT).show();
}else if(value>=751 && value<=810){
Toast.makeText(getApplicationContext(), "영화 !!!!", Toast.LENGTH_SHORT).show();
}
}}
ㅇ activity_roulette.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/point1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="V"
android:gravity="center"/>
<TextView
android:id="@+id/point2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/point1"
android:text="V"
android:gravity="center"/>
<ImageView
android:id="@+id/wheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:layout_centerInParent="true"
android:layout_below="@+id/point2"
android:src="@drawable/roulette"/>
<Button
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="start"/>
</RelativeLayout>
'Android' 카테고리의 다른 글
android-design-support-library (0) | 2019.04.18 |
---|---|
안드로이드 아키텍처 컴포넌트 (0) | 2018.12.30 |
Lock Pattern (0) | 2018.11.06 |
FingerPrint(지문인증) (0) | 2018.11.06 |
BarCode, QRCode&Scan (0) | 2018.11.06 |