Skip to content

Instantly share code, notes, and snippets.

@sourcebits-anshul
Created March 23, 2012 09:44
Show Gist options
  • Save sourcebits-anshul/2169073 to your computer and use it in GitHub Desktop.
Save sourcebits-anshul/2169073 to your computer and use it in GitHub Desktop.
Animation
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/baseView"
android:background="#FFFFFFFF"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/turntable"
android:src="@drawable/wheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal|center_vertical"
android:scaleType="center"/>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:text="00:00"
android:textColor="#000"
android:textSize="30sp" />
</FrameLayout>
package com.sourcebits.wheelAnimation;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
public class WheelAnimationActivity extends Activity{
View baseView;
ImageView turntable;
TextView timeText;
int hours = 0;
int minute = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
baseView = (View)findViewById(R.id.baseView);
turntable = (ImageView)findViewById(R.id.turntable);
timeText = ( TextView )findViewById(R.id.time);
turntable.setOnTouchListener(onTableTouched);
}
public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent evt)
{
double r = Math.atan2(evt.getX() - turntable.getWidth() / 2, turntable.getHeight() / 2 - evt.getY());
int rotation = (int) Math.toDegrees(r);
if (evt.getAction() == MotionEvent.ACTION_DOWN)
{
//
}
if (evt.getAction() == MotionEvent.ACTION_MOVE)
{ /*if( rotation < 0 ){
rotation = 360 + rotation;
}*/
Log.i("Anshul :", rotation+"");
updateRotation(rotation);
}
if (evt.getAction() == MotionEvent.ACTION_UP)
{
//
}
return true;
}
};
private void updateRotation(double rot)
{
//float newRot = new Float(rot);
Integer newRot = new Integer((int)rot);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wheel);
Matrix matrix = new Matrix();
matrix.postRotate( newRot );
if ((newRot <= 100 && newRot > 89)){
Log.i("New Degree 1:",newRot+"");
changeTime();
//matrix.postRotate(90);
}
else if((newRot <= 189 && newRot > 179)){
Log.i("New Degree 2:",newRot+"");
changeTime();
// matrix.postRotate(360);
}
else if((newRot <= -79 && newRot > -89)){
Log.i("New Degree 3:",newRot+"");
changeTime();
}
else if( newRot == 0 ){
Log.i("New Degree 4:",newRot+"");
changeTime();
}
/* if( newRot > 45 && newRot < 90){
Log.i("New Degree 1:",newRot+"");
//matrix.postRotate(90);
}
else if( newRot < 45 && newRot > 0){
Log.i("New Degree 2:",newRot+"");
// matrix.postRotate(360);
}
else if( newRot > 135 && newRot < 180){
Log.i("New Degree 3:",newRot+"");
//matrix.postRotate(180);
}
else if( newRot < 135 && newRot > 90 ){
Log.i("New Degree 4:",newRot+"");
//matrix.postRotate(90);
}
else if( newRot > 225 && newRot < 270){
Log.i("New Degree 5:",newRot+"");
//matrix.postRotate(-90);
}
else if( newRot < 225 && newRot > 180 ){
Log.i("New Degree 6:",newRot+"");
//matrix.postRotate(180);
}
else if( newRot > 315 && newRot < 360 ){
Log.i("New Degree 7:",newRot+"");
//matrix.postRotate(0);
}
else if( newRot < 315 && newRot > 270 ){
Log.i("New Degree 8:",newRot+"");
//matrix.postRotate(-90);
}
*/
Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
turntable.setImageBitmap(redrawnBitmap);
}
private void changeTime() {
if( minute <= 59 ){
minute = minute + 15;
Log.i("Anshul minute :",minute+"");
timeText.setText("00:"+minute+"");
}else{
if( hours == 0 || hours < 10 ){
int temp = minute / 60;
hours = hours + temp ;
Log.i("Anshul hours :",hours+"");
timeText.setText("0"+hours+":00");
minute = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment