android imgeview pause and clear - android

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

Related

How to make visible and invisible an image by clicking a button in android studio?

i have two activities in android studio.act1 with a button and act2 with an imageView. i want to click the button in act1 and make the image in act2 visible. and when i click button for second time, this time make the image invisible and do it again and again and again. how can i do that?
you must create this class;
public class PublicSharedPreferences {
public static void setDefaults(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
}
and then learn sharedpreferences enter link description here
Activity1;
public class Activity1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(Activity1.this, "it visibled", Toast.LENGTH_SHORT).show();
visibilityStr = "1";
} else {
visibilityStr = "0";
Toast.makeText(Activity1.this, "it invisibled", Toast.LENGTH_SHORT).show();
}
} else {
visibilityStr = "1";
Toast.makeText(Activity1.this, "it visibled", Toast.LENGTH_SHORT).show();
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
Intent intent = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(intent);
}
});
}
}
Activity2;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ImageView imgView = (ImageView) findViewById(R.id.imgView1);
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView.setVisibility(View.INVISIBLE);
else
imgView.setVisibility(View.VISIBLE);
}
}
Layout1;
<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.c.a.myapplication.Activity1">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
</LinearLayout>
Layout2;
<?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:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_camera"
android:visibility="invisible"/>
</LinearLayout>
Its work.
Try this code....
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Sample extends Activity {
ImageView img;
Button btn;
boolean clicked = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
btn = (Button) findViewById(R.id.t1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked = true;
Intent intent = new Intent(Sample.this, Dample.class);
intent.putExtra("value", true);
startActivity(intent);
}
});
}
}
//Dample.class In second activity
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
public class Dample extends Activity {
ImageView img;//use image view
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pimple);
img= (ImageView) findViewById(R.id.t1);
Boolean yourBool = getIntent().getExtras().getBoolean("value");
if (yourBool == true) {
img.setVisibility(View.VISIBLE);///use visibility code for imageview as mentioned above
}
}
}

Android continuous moving 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>

Animation Translate using LayoutParams

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);

ViewFlipper causing null pointer exception

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);

Android imageView Switching Problem

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>

Categories

Resources