Basically i want to create translate animation but for some reason i cannot use TranslateAnimation class. Anyway this is my code
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
</RelativeLayout>
MainActivity.java
package com.test2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ImageView;
public class MainActivity extends Activity
{
private RelativeLayout layout;
private ImageView image;
private LayoutParams imageParams;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (RelativeLayout) findViewById(R.id.layout);
image = (ImageView) findViewById(R.id.image);
imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
image.setLayoutParams(imageParams);
layout.setOnClickListener(new ClickHandler());
}
class ClickHandler implements OnClickListener
{
public void onClick(View view)
{
// This is the animation part
for (int i = 0; i < 100; i++)
{
imageParams.leftMargin = i;
image.setLayoutParams(imageParams);
// repaint() in java
try
{
Thread.sleep(100);
}
catch (Exception e)
{
}
}
}
}
}
My problem is I need to do repaint() before using Thread.sleep otherwise the animation will not work and the object will just move to its final position. I've tried invalidate, requestLayout, requestDrawableState but none works. Any suggestion?
EDIT:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
private RelativeLayout layout;
private ImageView image;
private LayoutParams imageParams;
private Timer timer;
private int i;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (RelativeLayout) findViewById(R.id.layout);
image = new ImageView(this);
image.setImageResource(R.drawable.ic_launcher);
imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
image.setLayoutParams(imageParams);
layout.addView(image);
i = 0;
timer = new Timer();
runOnUiThread
(
new Runnable()
{
public void run()
{
timer.scheduleAtFixedRate
(
new TimerTask()
{
#Override
public void run()
{
imageParams.leftMargin = i;
image.setLayoutParams(imageParams);
i++;
if (i == 15)
{
timer.cancel();
timer.purge();
}
}
}, 0, 100
);
}
}
);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
package com.example.example;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;
public class MainActivity extends Activity {
ImageView imageView;
RelativeLayout.LayoutParams params;
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=new ImageView(this);
RelativeLayout main=(RelativeLayout)findViewById(R.id.main);
params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setBackgroundResource(R.drawable.ic_launcher);
imageView.setLayoutParams(params);
main.addView(imageView);
final Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
params.leftMargin=i;
imageView.setLayoutParams(params);
i++;
if(i>=100){
timer.cancel();
timer.purge();
}
}
});
}
}, 0, 100);
}
}
you can use timer task instead of sleep
Like:-
int i=0;
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
imageParams.leftMargin = i;
image.setLayoutParams(imageParams);
i++;
if(i==100){
timer.cancel();
timer.purge();
}
}
}, 0, 1000);
Related
I've created ProgressBar in my view, and I would like it to animate when it's value is updated. I'm runnig the code on API 23. How to animate progressBar when It's value is updated?
Here is my activity:
package com.brylkowski.cleaner.cleaner.Errands;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
import com.brylkowski.cleaner.cleaner.R;
public class UploadErrandActivity extends AppCompatActivity {
public static final String PROGRESS = "progress";
UploadRequestTask task;
private ProgressBar progressBar;
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_errand);
progressBar = (ProgressBar) findViewById(R.id.upload_progress);
progressBar.setMax(6);
task = new UploadRequestTask();
handler = new Handler() {
public void handleMessage(android.os.Message msg) {
progressBar.incrementProgressBy(1);
}
};
task.execute();
}
public class UploadRequestTask extends AsyncTask<Void, Void, Boolean> {
UploadRequestTask() {
}
#Override
protected Boolean doInBackground(Void... params) {
for (int i = 1; i <=6; i++){
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putInt(PROGRESS, i);
msg.setData(bundle);
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return true;
}
#Override
protected void onPostExecute(final Boolean success) {
}
#Override
protected void onCancelled() {
}
}
}
and my view:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center"
tools:context="com.brylkowski.cleaner.cleaner.Errands.UploadErrandActivity">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/upload_progress"
android:layout_gravity="bottom"
android:longClickable="false"/>
</LinearLayout>
Check this for how to make ProgressBar animation. The only modification you need is instead of applying the animation once when view is initialized, you will need to call
ProgressBarAnimation anim = new ProgressBarAnimation(progressBar, progressBar.getProgress(), to);
anim.setDuration(1000);
progressBar.startAnimation(anim); // play animation immediately
every time you want to update value. Here is documentation for view animation.
I want to do an animation in which an image continuously keeps moving from top to bottom after every 3 seconds. I have achieved this using the following code
My XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top"
android:baselineAligned="false"
android:background="#ffffff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout1">
</RelativeLayout>
</LinearLayout>
My Activity class
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.Timer;
import java.util.TimerTask;
public class GameActivity extends AppCompatActivity {
private RelativeLayout layout1;
private TranslateAnimation moveDownwards;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout1 = (RelativeLayout) findViewById(R.id.layout1);
moveDownwards = new TranslateAnimation(0, 0, -100, 1000);
moveDownwards.setDuration(3000);
moveDownwards.setFillAfter(true);
startTimer();
}
#Override
protected void onResume() {
super.onResume();
startTimer();
}
private void startTimer() {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
ImageView iv1 = new ImageView(GameActivity.this);
iv1.setImageResource(R.drawable.c_orange);
layout1.addView(iv1);
iv1.startAnimation(moveDownwards);
}
});
}
};
timer.schedule(task,0,3000);
}
}
Above code is working fine but the animation is not consistent. For example, when I touch the screen while the image is moving, the smoothness of animation is disturbed and it becomes rough.
Is there any better way to achieve this???
There is no need to add a new Image view after every 3 sec. You can set repeatcount -1(INFINITE).
public class MainActivity extends Activity {
private TranslateAnimation moveDownwards;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moveDownwards = new TranslateAnimation(0, 0, -100, 1000);
moveDownwards.setDuration(3000);
moveDownwards.setFillAfter(true);
moveDownwards.setRepeatCount(-1);
findViewById(R.id.image).startAnimation(moveDownwards);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start|top"
android:background="#ffffff" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I have a program which uses view flipper I am getting a NullPointerException as follows when the activity is called. I wish to cycle through a set of images as long as the activity is being run.
06-13 18:52:05.358: E/AndroidRuntime(368): Caused by: java.lang.NullPointerException
Activity:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class MainMenuActivity extends Activity{
Handler handler;
Runnable runnable;
ViewFlipper imageSwitcher;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menupage);
imageSwitcher= (ViewFlipper) new ViewFlipper(this).findViewById(R.id.sportspersons);
this.viewchanger();
}
public void viewchanger()
{
imageSwitcher.setInAnimation(this, R.anim.fadein);
imageSwitcher.setOutAnimation(this, R.anim.fadeout);
ImageView i = new ImageView(this);
i.setBackgroundDrawable(getResources().getDrawable(R.drawable.jowens));
ImageView i2 = new ImageView(this);
i2.setBackgroundDrawable(getResources ().getDrawable(R.drawable.maradonna));
ImageView i3 = new ImageView(this);
i2.setBackgroundDrawable(getResources().getDrawable(R.drawable.mjordan));
ImageView i4 = new ImageView(this);
i2.setBackgroundDrawable(getResources().getDrawable(R.drawable.pele));
imageSwitcher.addView(i);
imageSwitcher.addView(i2);
imageSwitcher.addView(i3);
imageSwitcher.addView(i4);
runnable = new Runnable() {
#Override
public void run() {
handler.postDelayed(runnable, 3000);
imageSwitcher.showNext();
}
};
handler.postDelayed(runnable, 500);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="508dp"
android:layout_x="-4dp"
android:layout_y="-5dp"
android:scaleType="fitXY"
android:src="#drawable/mainmenu01" />
<ViewFlipper
android:id="#+id/sportspersons"
android:layout_width="86dp"
android:layout_height="91dp"
android:layout_x="38dp"
android:layout_y="373dp" />
</AbsoluteLayout>
You can try this way:
runnable = new Runnable() {
#Override
public void run() {
handler.postDelayed(runnable, 3000);
imageSwitcher.setAutoStart(true);
imageSwitcher.startFlipping();
}
};
Change
imageSwitcher= (ViewFlipper) new ViewFlipper(this).findViewById(R.id.sportspersons);
To
imageSwitcher= (ViewFlipper) findViewById(R.id.sportspersons);
To add to Waqas Point.
You have serious issue in following piece of code.
runnable = new Runnable() {
#Override
public void run() {
handler.postDelayed(runnable, 3000);
imageSwitcher.showNext();
}
};
handler.postDelayed(runnable, 500);
I have this code and I'm trying to read 2 images from the SD card and I don't know how to pause between the 2 images that I can have enough time to see each 1 of them. Also, I don't know how to clear the ImgeView after I display the 2 images
This is how I want it to go:
load first image -> wait for 5 sec -> load sec image-> wait for 5 sec clear the ImageView
MainActivity.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class ReadfromSD extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String imageInSD = "/sdcard/Hanud/AD2.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView) findViewById(R.id.imageview1);
myImageView.setImageBitmap(bitmap);
String imageInSD2 = "/sdcard/Hanud/AD1.jpg";
Bitmap bitmap2 = BitmapFactory.decodeFile(imageInSD2);
ImageView myImageView1 = (ImageView) findViewById(R.id.imageview1);
myImageView1.setImageBitmap(bitmap2);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="center" />
</LinearLayout>
public class ImgLoadAct extends Activity {
/** Called when the activity is first created. */
Timer timer;
ImageView myImageView;
TextView titleText;
Drawable img1Drawable;
Drawable img2Drawable;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView = (ImageView)findViewById(R.id.imageview1);
titleText=(TextView)findViewById(R.id.img_title);
img1Drawable=getResources().getDrawable(R.drawable.android_logo);
img2Drawable=getResources().getDrawable(R.drawable.apple_logo);
//Load Img1 android_logo
myImageView.setImageDrawable(img1Drawable);
titleText.setText("Img2 android_logo.png loaded");
timer=new Timer();
//delay amount of time(5s here) in milliseconds before first execution.
timer.schedule(loadImg2, 5000);
}
TimerTask loadImg2 = new TimerTask(){
#Override
//Load Img2
public void run() {
runOnUiThread(new Runnable(){
public void run() {
titleText.setText("Img2 apple_logo.png loaded");
myImageView.setImageDrawable(img2Drawable);
timer.cancel();
timer=new Timer();
timer.schedule(clearImg, 5000);
}
});
}
};
TimerTask clearImg = new TimerTask(){
#Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable(){
public void run() {
timer.cancel();
titleText.setText("Img cleared");
myImageView.setImageDrawable(null);
}
});
}
} ;
}
Src dowload:
http://www.everbox.com/f/UtxQ4gzfXBPTFZlcIXi3HQHGU4
i have imageview and i have to display different image in
that with time interval, but when i change the ImageResource
the last image is displayed
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test);
try {
Thread.sleep(2000) ;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
}
Kindly Suggest the Right way to do that
Thanks in Advance
Perhaps this thread is what you are looking for:
how to change images with timer
Whenever you want to update the user interface without performing any action or event, handlers should be used.
This is the sample code
Main.java
package com.balaji.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
private ImageView txtStatus;
int i=0;
int imgid[]={R.drawable.icon,R.drawable.back,R.drawable.slider,R.drawable.forward};
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
#Override
public void handleMessage(Message msg) {
Main.this.updateUI();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
private void updateUI(){
//int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;
if(i<imgid.length){
mRedrawHandler.sleep(1000);
txtStatus.setBackgroundResource(imgid[i]);
i++;
}
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.txtStatus = (ImageView)this.findViewById(R.id.txtStatus);
updateUI();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</ImageView>
</RelativeLayout>