Image rotation Android - android

I am working on android. I want to create a rotating image and select parts on it. For example if the image is a human head then while its rotating, the ears can be selected. Now I did the image part selection, but I don't know how to rotate it? Can somebody show me an easy way?

You have 2 ways to rotate a image, i put the simple with xml.
First inside directory res create a directory with the name anim
inside the directory anim create a file xml with this code:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
>
</rotate>
this code is the instruction to make a image rotate.
finally your main activity must look like that:
public class MainActivity extends Activity implements OnClickListener{
ImageView imagen; //declare the image will use
Button boton; // this button will activie de rotation
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagen = (ImageView) findViewById(R.id.iv);
boton = (Button)findViewById(R.id.bt);
boton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt:
Animation rotacion; // declare an animation
rotacion = AnimationUtils.loadAnimation(this,R.anim.rotar);
rotacion.reset();
imagen.startAnimation(rotacion);
break;
default:
break;
}
}

You should use http://developer.android.com/reference/android/view/animation/RotateAnimation.html class to make animation and animate your view

Related

Animation will NOT stop running

i have implemented an animation on one of my image views. My problem is that the animation will NOT stop at all. I call everything clearanimation i set it to null set it to cancel and it still wont stop.
public void tiltani(){
ImageView vault = (ImageView)findViewById(R.id.vault2) ;
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.startAnimation(tilt);
}
public void stopani() {
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
here is xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="6"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatCount="infinite"/>
<rotate
android:fromDegrees="6"
android:toDegrees="-2"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
here is where i start it
Intent intent1 = getIntent();
if (intent1.hasExtra("id1")) {
tiltani();
and i try to stop/cancel everything in an onclick method
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.chestopen));
stopani();
update
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
if (intent1.hasExtra("id1")) {
vault.startAnimation(tilt);
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,
R.drawable.chestopen));
vault.setAnimation(null);
You can just use vault.startAnimation(tilt); to start your animation, and use vault.setAnimation(null); to stop your animation.
You have initialized Animation a second time in stopani() method. Your view initialization and animation initialization should be global.
Your code should be like this.
ImageView vault = (ImageView)findViewById(R.id.vault2); //Global variable
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt); //Global variable
public void tiltani(){
vault.startAnimation(tilt);
}
public void stopani() {
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
Hope, It will help you.. :)

Glow button on and off

I have two images which I want to fade between causing a glowing on and off effect. This should run all the time like an animation, not just when the button is pressed.
Here are the two images:
This was working well before but after some hardware/android OS updates my animation is really jumpy. Here is the Animation XML I was using:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/bottom_bar_add_dark"/>
<item android:drawable="#drawable/bottom_bar_add" android:duration="500" />
</animation-list>
I have looked high and low and cannot find an answer to this.
Edit
Here is the code that creates the image view and sets all its resources:
public ImageView findDevicesButton(){
bottomButton = new ImageView(this);
int id = bottomButton.generateViewId();
bottomButton.setId(id);
if(currentapiVersion >= 11){
bottomButton.setImageResource(R.drawable.animationxmladddevice);
//Background image
bottomButton.setBackgroundResource(R.drawable.bottom_bar);
saveButtonAnimation = (AnimationDrawable)bottomButton.getDrawable();
saveButtonAnimation.setEnterFadeDuration(1000);
saveButtonAnimation.setExitFadeDuration(1000);
bottomButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
findDevicesAlertBuilder();
}
});
}else{
bottomButton.setImageResource(R.drawable.bottom_bar_add);
bottomButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
bottomButton.setBackgroundResource(R.drawable.bottom_bar);
bottomButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
findDevicesAlertBuilder();
}
});
}
return bottomButton;
}
This is the back background image:
All together it should look like this with the center button glowing:

changing images through ling click in android

i am making a flashlight app in which i am using using two images, one each for flashoff state and flashon state respectively.
i want to have animation such that these two images get toggled(image 1 apperas then image2 then again image 2..infinite times) when there is longclick..
that is iwant to show this in longclicklistner...
here is what i have done..
i am using this animation..
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:duration="1000" android:repeatCount="infinite" android:repeatMode="reverse" android:fromAlpha="0.3" android:toAlpha="1.0" />
and
#Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
tButton.startAnimation(flickon); //tbutton is an image button
return true;
}
});
}
using this animation is happening but images are not switching..please help
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.xxx);
use the above code to set an image to the image view and use a thread that can change contents in the UI bascially some thing like this
public void onLongClick(View v) {
new Thread(new Runnable() {
public void run() {
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.xxx);
}
}).start();
}
You can loop over it for as many times as you want.

Implementing slide transition after a option item selected android

I build a action bar.Which contain a item say setting (image).After clicking on the image the another activity is being started.
But I want the starting activity to show sliding transition effect.current activity slide left and hide and starting activity should slide from right to left.
here is code
code for selecting the item
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.setting:
goToSetting();
}
return super.onOptionsItemSelected(item);
}
gotoSetting function :
public void goToSetting() {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageSetting.class);
startActivity(intent);
}
which is calling activity named "DisplayMessageSetting"
please help
Edit the onCreate method in your DisplayMessageSetting activity like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//specify the animation
overridePendingTransition(R.anim.slide_left_in, 0);
//...
}
Create a slide_left_in.xml file in your res/anim folder (create the folder if it doesn't exist) and paste the following :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="400"/>
</set>
This way you just specified an animation set with one translate animation in it.
You have a lot more options to tweak that animation, you can learn a bit more about that here.

Animation is not working in Emulater

i am enable to load animation in Emulater..Its working fine with any real device..
public class MainActivity extends Activity {
private ImageView imgView;
private Animation animation;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.grow);
animation.setRepeatCount(50); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE);
imgView = (ImageView) findViewById(R.id.imgView);
imgView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent it = new Intent(getApplicationContext(), MyWebView.class);
startActivity(it);
}
}, 5000);
}
});
}
and my anim xml file is following
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="50" />
I think you Might have Disabled Animations in Emulator:
Check that:
Settings>Display>Animation..
Hope that helps your Problem
For anyone running into similar issues when using ObjectAnimator, in my case I had to enable the developer settings on the emulator and then go into "Settings" > "Developer options" > "Drawing" section
In this "Drawing" section you'll find different options for each animation type, in my case the "Animator duration scale" was off, after setting it to "1x" I started seeing the animations on the emulator.

Categories

Resources