I'm trying to animate an imageview in height, with a var that I'll receive from a database later on. Checked stack and other sites but did not find a suitable answer, do not want do it using xml. here is some code i already have:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/enkelzijdig" />
<ImageView
android:id="#+id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_marginTop="150dp"
android:src="#drawable/animatiebalk" />
</RelativeLayout>
**than where the magic should happen:**
package com.example.grafiek;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
public class MainActivity extends Activity{
ImageView balk1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
balk1 = (ImageView)findViewById(R.id.balk1);
}
public void ScaleAnimation (float fromX, float toX, float fromY, float toY){
no clue what to do
}
}
Use the animation to do that---
Animation fadeInFromTopAnimation = AnimationUtils
.loadAnimation(ActivityName.this, R.anim.slide_up);
fadeInFromTopAnimation
.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim) {
}
public void onAnimationEnd(Animation anim) {
imageView.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation anim) {
}
});
imageView.startAnimation(fadeInFromTopAnimation);
where you have to make the following slide_up.xml in anim folder---
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "#android:anim/linear_interpolator">
<alpha
android:fromAlpha = "1"
android:toAlpha = "0"
android:duration = "100">
</alpha>
<scale
android:fromXScale = "1"
android:fromYScale = "1"
android:toXScale = "1"
android:toYScale = "0"
android:pivotX = "50%"
android:pivotY = "0%"
android:duration = "100">
</scale>
</set>
Related
I am creating a simple Android app, in which the user can drag a SeekBar to the left to reveal less of an image, and right to reveal more of an image. This is accomplished through attaching a listener to the SeekBar, in which the width of the ImageView is changed proportionally to the SeekBar's progress. The problem is that no matter what position the slider is in, the width of the ImageView remains the same, as shown in the screenshots below:
Progress = 0%:
Progress = 100%
This seems strange, considering that in the SeekBar's listener, when I output the value I am setting the ImageView width to, that value is correct.
Below is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="300px"
android:layout_width="300px"
android:orientation="vertical"
android:background="#0000FF">
<ImageView
android:layout_height="300px"
android:layout_width="300px"
android:src="#drawable/android"
android:scaleType="matrix"
android:id="#+id/iv"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.example.changingimageviewwidth2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = findViewById(R.id.iv);
SeekBar seekbar = findViewById(R.id.simpleSeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv.getLayoutParams().width = 300 * progress/100;
System.out.println(300 * progress/100);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//write custom code to on start progress
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Note: The image is NOT supposed to be resized when the slider is moved.
This could be done with an additional View on top of the layout hierarchy with the same size as ImageView. We dynamically change the top View startMargin to have an image revealing/hiding effect.
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.iv);
final View dynamicView = ((View) findViewById(R.id.dynamicView));
final SeekBar seekbar = findViewById(R.id.simpleSeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) dynamicView.getLayoutParams();
params.setMarginStart(imageView.getWidth() * progress / 100);
dynamicView.setLayoutParams(params);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="300dp"
android:layout_width="300dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="#drawable/owl"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/dynamicView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Code: https://github.com/dautovicharis/sos_android/tree/q_68375499
Additional:
Instead of px use dp in layouts
My suggestion is to switch to Kotlin
ConstraintLayout is a better option if you want to have better performance and avoid nested views
EDIT:
I was not happy with the first solution and did a small research. A better way is to do it using ImageView overlay.
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.iv);
final SeekBar seekbar = findViewById(R.id.simpleSeekBar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
addOverlay(R.drawable.overlay, imageView, imageView.getWidth() * progress / 100);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// Initial overlay
imageView.post(() -> addOverlay(R.drawable.overlay, imageView, 0));
}
private void addOverlay(int resourceId, ImageView imageView, int startMargin) {
imageView.getOverlay().clear();
Drawable drawable = ResourcesCompat.getDrawable(getResources(), resourceId, null);
drawable.setBounds(new Rect(startMargin, 0, imageView.getWidth(), imageView.getHeight()));
imageView.getOverlay().add(drawable);
}
}
res->drawable-overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" />
</layer-list>
res->layout->activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="#+id/simpleSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="matrix"
android:src="#drawable/owl"
/>
</LinearLayout>
Code: https://github.com/dautovicharis/sos_android/commits/q_68375499_v2
EDIT: Final solution and what Adam actually wanted to achieve is:
Code: https://github.com/dautovicharis/sos_android/tree/q_68375499_v3
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv.getLayoutParams().width = 300 * progress/100;
iv.drawableStateChanged(); //<--- You need to redraw it after you change it.
System.out.println(300 * progress/100);
}
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
iv.getLayoutParams().width = 200 * progress;
iv.getLayoutParams().height= 200 * progress;
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//write custom code to on start progress
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I'm a beginner to the android development. I have an android image view that import web image by using Picasso 2.5.2 library. It's working. After click on that imageview, I want to create a full screen Android dialog with that image. I' used following code for it. But after click on the image view, a dialog is displayed without full screen. Finally, I want to zoom in and zoom out that full screen dialog. I want this kind of full screen on imageview onClickListener. Also see how the below image has a title and I also need the image caption (like in Facebook)
Here is my code
Mainactivity.java
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
ImageView imageView1;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
}
public void initUi(){
imageView1 = (ImageView)findViewById(R.id.image);
Picasso.with(this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.into(imageView1);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showImage();
}
});
}
public void showImage() {
Dialog builder = new Dialog(this, android.R.style.Theme_Light);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(this);
Picasso.with(this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.into(imageView);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
builder.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_add_info_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="Image buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
You can use Chrisbane's PhotoView lib for zooming the image.
Open the detail page by sending the path,
public void showImage() {
Intent intent = new Intent(this, ImageDeatilActvity.class);
intent.putExtra("path", "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");
startActivity(intent);
}
Complete ImageDeatilActvity.java
public class ImageDeatilActvity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener {
private boolean mIsFullScreen;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_deatil_actvity);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
mIsFullScreen = true;
String path = getIntent().getStringExtra("path");
PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
#Override
public void onPhotoTap(View view, float x, float y) {
updateView();
}
#Override
public void onOutsidePhotoTap() {
updateView();
}
});
Glide.with(this).load(path).into(photoView);
}
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
mIsFullScreen = false;
}
updateView();
}
public void updateView() {
mIsFullScreen = !mIsFullScreen;
if (mIsFullScreen) {
hideSystemUI();
} else {
showSystemUI();
}
}
private void hideSystemUI() {
mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();
}
private void showSystemUI() {
mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Detail Activity Layout,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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="in.muthu.stackoverflow.ImageDeatilActvity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#33000000"
android:fitsSystemWindows="true"
app:contentScrim="#android:color/transparent"
app:popupTheme="#style/AppTheme.AppBarOverlay"/>
<uk.co.senab.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
In My example, I am using Glide not Picaso, because Android
official recommendation is Glide, If you want you can change that.
set image to centre crop and resize it with your screen size like
Picasso.with(MainActivity.this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Use below xml for layout, i added one more ImageView just use make the visibility visible on click of small image_view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_add_info_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="Image buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"/>
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
I'm making a text-based game with Android Studio.
This is the Activity:
public class MainActivity extends Activity {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt1 = (TextView)findViewById(R.id.textView5);
final TextView txt3 = (TextView)findViewById(R.id.textView6);
final TextView txt2 = (TextView)findViewById(R.id.textView7);
final TextView txt4 = (TextView)findViewById(R.id.textView8);
final ImageView img1 = (ImageView)findViewById(R.id.imageView2);
new CountDownTimer(2000, 1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt1.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt2.startAnimation(fadeIn2);
txt1.setText("Text");
txt1.setVisibility(View.VISIBLE);
txt2.setText("Text");
txt2.setVisibility(View.VISIBLE);
}
}.start();
new CountDownTimer(5000, 1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
Animation fadeIn_img = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
img1.startAnimation(fadeIn_img);
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt3.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt4.startAnimation(fadeIn2);
img1.setImageResource(R.drawable.img);
txt3.setText("Text");
txt3.setVisibility(View.VISIBLE);
txt4.setText("Text");
txt4.setVisibility(View.VISIBLE);
}
}.start();
I want the animation and the countdowntimer to work only one time for each textview. When I restart the game the textviews have to display without anything.
It is like a list of textviews that display in succession every 2 seconds. When the user close and then restart the application, he has to find the state where he had finished.
How can I do that?
Thanks.
So it looks like a fainting textviews right? Heres an example
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
LinearLayout Container;
ArrayList<View> textList = new ArrayList<>();
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Container = (LinearLayout) findViewById(R.id.Container);
for (int i = 0; i < Container.getChildCount(); i++) {
textList.add(Container.getChildAt(i));
}
fadeView(textList.get(0));
}
private void fadeView(final View view) {
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fadein);
count++;
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
if (count == textList.size()) return;
fadeView(textList.get(count));
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
view.startAnimation(anim);
}
}
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1.0"
android:repeatCount="0"
android:toAlpha="0.0" />
XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is first"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is second"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is third"
android:textSize="30dp" />
</LinearLayout>
Just make sure to put all your views (in this case textviews) inside an array to make it clean and proper.
Hope it helps. Cheers Happy codings.
I'm new at Android Programing and I want to know how to rotate an image by a random angle. I need this for my "Spin the Bottle" game. Here is my code so far:
package example.com.bottlegame;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.RotateDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import java.lang.Object;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
ImageView spin,bottle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (ImageView) findViewById(R.id.ivSpin);
bottle = (ImageView) findViewById(R.id.ivBottle);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.roatate);
spin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottle.startAnimation(animRotate);
}
});
}
}
Now I'm rotating an image with XML, but can you help me to rotate it with just Java code. Here is the XML code in anim folder:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:drawable="#drawable/bottle"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
/>
</set>`
And here is the activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/bg_background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100sp"
android:layout_centerHorizontal="true"
android:id="#+id/ivSpin"
android:src="#drawable/spin_up"
android:layout_marginTop="400sp"
android:contentDescription="#string/spin"
/>
<ImageView
android:layout_width="65sp"
android:layout_height="200sp"
android:id="#+id/ivBottle"
android:src="#drawable/bottle"
android:layout_marginTop="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/bottle"
/>
</RelativeLayout>
You can easily configure your animation in Java code as well as in xml. For your case you could use class RotateAnimation and its' constructor public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
So, try somehting like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (ImageView) findViewById(R.id.ivSpin);
bottle = (ImageView) findViewById(R.id.ivBottle);
float toDegrees = new Random().nextFloat() * Integer.MAX_VALUE % 360;
final Animation animRotate = new RotateAnimation(0, toDegrees, 50, 50);
animRotate.setDuration(1000);
animRotate.setRepeatCount(1);
animRotate.setRepeatMode(Animation.REVERSE);
spin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottle.startAnimation(animRotate);
}
});
}
I think, that no comments are required.
I am doing an android project to create an animation using eclipse.
On MainActivity.java
it compiler shows 5 lint errors as follows
-image cannot be resolved or is not a field
-activity_main cannot be resolved or is not a field
-translate cannot be resolved or is not a field
-rotate cannot be resolved or is not a field
-shape cannot be resolved or is not a field
Here is doing there my code in MainActivity.java
package ahmed103.appdemo;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.*;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements AnimationListener {
ImageView image;
Animation animation1;
Animation animation2;
Button rotate, translate;
Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
rotate = (Button) findViewById(R.id.rotate);
translate = (Button) findViewById(R.id.translate);
image.setImageResource(R.drawable.shape);
animation1 = AnimationUtils.loadAnimation(this, R.anim.rotate);
animation2 = AnimationUtils.loadAnimation(this, R.anim.translate);
userInputHandler();
}
private void userInputHandler() {
rotate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
image.startAnimation(animation1);
}
});
translate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
image.startAnimation(animation2);
}
});
}
#Override
public void onAnimationEnd(Animation animation) {
image.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
image.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationStart(Animation animation) {
image.setVisibility(View.VISIBLE);
}
}
Here is down there my activity_main.xml file
<html>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="70dp"
android:minHeight="150dp"
android:minWidth="150dp" />
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableRow>
<Button
android:id="#+id/rotate"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:text="#string/rotate_text" />
<Button
android:id="#+id/translate"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:text="#string/translate_text" />
</LinearLayout>
</html>
Remove the below import
import android.R;
Also you have
<html> and </html>// should be removed from xml
Imnport
import ahmed103.appdemo.R;
And use
<?xml version="1.0" encoding="utf-8"?>
as the first statement in your activity_main.xml