How to randomly rotate an image in Android? - java

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.

Related

I was trying to make a todo app with using class system but I get this error

error : "Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference"
so what should ı do to fix it if someone help me ı'll be so gratefull
here is my codes
cardbg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#color/purple_500"
android:id="#+id/cardbg">
</LinearLayout>
activit_ymain.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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=".MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical" />
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.thehuman.todo;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout main;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = findViewById(R.id.main);
card first = new card(MainActivity.this);
main.addView(first);
}
}
class card extends LinearLayout{
card self;
LinearLayout bg,main;
Button clon,delete;
#SuppressLint("ResourceAsColor")
public card(Context context) {
super(context);
self=this;
main = findViewById(R.id.main);
bg = findViewById(R.id.cardbg);
clon = new Button(context);
clon.setBackgroundColor(R.color.white);
clon.setPadding(20,20,20,20);
clon.setTextColor(R.color.teal_200);
clon.setText("clon me");
delete = new Button(context);
delete.setBackgroundColor(R.color.black);
delete.setTextColor(R.color.teal_200);
delete.setPadding(20,20,20,20);
delete.setText("delete me");
bg.addView(clon);
bg.addView(delete);
clon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
card cloncard = new card(context);
main.addView(cloncard);
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
main.removeView(self);
}
});
}
}
I suppose you are getting null object reference error in class Card. Because you are using bg = findViewById(R.id.cardbg);. You can not find cardbg.xml using findViewById because cardbg.xml have not been attached to you activity layout. You have to inflate your xml by yourself using an inflater.
Use
bg = LayoutInflater.from(context).inflate(R.layout.cardbg, null).findViewById(R.id.cardbg);

Lottie Animation not working with setExitSharedElementCallback android

I am working on Android with a Lottie animation. I implemented a shared transition on Lottie from one activity to another activity. But this is not Working. What am I doing wrong in my setExitSharedElementCallback method?
MainActivity.java
package com.example.trans;
import android.app.ActivityOptions;
import android.app.SharedElementCallback;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.airbnb.lottie.LottieAnimationView;
public class MainActivity extends AppCompatActivity {
LottieAnimationView lottieAnimationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lottieAnimationView = findViewById(R.id.animation_logo);
setExitSharedElementCallback(new SharedElementCallback() {
#Override
public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) {
int bitmapWidth = Math.round(screenBounds.width());
int bitmapHeight = Math.round(screenBounds.height());
Bitmap bitmap = null;
if (bitmapWidth > 0 && bitmapHeight > 0) {
Matrix matrix = new Matrix();
matrix.set(viewToGlobalMatrix);
matrix.postTranslate(-screenBounds.left, -screenBounds.top);
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.concat(matrix);
sharedElement.draw(canvas);
}
return bitmap;
}
});
findViewById(R.id.start_animation).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, lottieAnimationView, "simple_activity_transition");
startActivity(intent, options.toBundle());
}
});
}
}
activity_main.xml
<?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"
android:background="#000"
tools:context=".MainActivity">
<Button
android:id="#+id/start_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/animation_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="click Me" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:transitionName="simple_activity_transition"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_rawRes="#raw/dm"
app:lottie_renderMode="hardware"
app:lottie_speed="1.2"
tools:ignore="MissingConstraints" />
</RelativeLayout>
activityMain2.java
package com.example.trans;
import androidx.appcompat.app.AppCompatActivity;
import android.app.SharedElementCallback;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import com.airbnb.lottie.LottieAnimationView;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setEnterSharedElementCallback(new SharedElementCallback() {
#Override
public View onCreateSnapshotView(Context context, Parcelable snapshot) {
View view = new View(context);
view.setBackground(new BitmapDrawable((Bitmap) snapshot));
return view;
}
#Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
ImageView sharedElement = findViewById(R.id.animation_logo);
for (int i = 0; i < sharedElements.size(); i++) {
if (sharedElements.get(i) == sharedElement) {
View snapshot = sharedElementSnapshots.get(i);
Drawable snapshotDrawable = snapshot.getBackground();
sharedElement.setBackground(snapshotDrawable);
sharedElement.setImageAlpha(0);
forceSharedElementLayout();
break;
}
}
}
private void forceSharedElementLayout() {
ImageView sharedElement = findViewById(R.id.animation_logo);
int widthSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getWidth(),
View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getHeight(),
View.MeasureSpec.EXACTLY);
int left = sharedElement.getLeft();
int top = sharedElement.getTop();
int right = sharedElement.getRight();
int bottom = sharedElement.getBottom();
sharedElement.measure(widthSpec, heightSpec);
sharedElement.layout(left, top, right, bottom);
}
#Override
public void onSharedElementEnd(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
ImageView sharedElement = findViewById(R.id.animation_logo);
sharedElement.setBackground(null);
sharedElement.setImageAlpha(255);
}
});
}
}
activity_main2.xml
<?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:background="#000"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_logo"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:transitionName="simple_activity_transition"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_rawRes="#raw/dm"
app:lottie_renderMode="hardware"
app:lottie_speed="1.2" />
</RelativeLayout>
Styles
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/explode</item>
</style>

Android: Display images in full screen with caption on `onClick` event

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>

image cannot be resolved or is not a field

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

Animate Height of my ImageView

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>

Categories