Image slide show in Android - android

I am making an application in android for picture slide show. I have tried the following code:
class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawable backgrounds[] = new Drawable[2];
Resources res = getResources();
backgrounds[0] = res.getDrawable(android.R.drawable.btn_star_big_on);
backgrounds[1] = res.getDrawable(android.R.drawable.btn_star_big_off);
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(crossfader);
crossfader.startTransition(3000);
}
}
This code successfully bring the new image on top but previous image is not faded out.
Can anyone help me on it?

You need to enable the cross fade on your transition to have both of your images fade. Use the method setCrossFadeEnabled to enable it by setting it tu true.
See documentation here http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html#setCrossFadeEnabled(boolean)

Related

How to make animation of image change for ImageView from Java code

I wanted to make it with TransitionDrawable class, but it needs a separate file transition.xml. There I define which image I change to.
I need to define them in Java code because I don't know which images I will change too. I have many images and I accidentally get only two images which will change between each other. What can I do? Perhaps I need another class.
Code with transition.xml:
public class TransitionActivity extends Activity
implements OnClickListener {
private ImageView image;
private TransitionDrawable mTransition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
image.setOnClickListener(this);
Resources res = this.getResources();
mTransition = (TransitionDrawable)res.getDrawable(R.drawable.transition);
}
#Override
public void onClick(View v) {
image.setImageDrawable(mTransition);
mTransition.startTransition(1000);
}
}
You can programmatically create a TransitionDrawable using the class' constructor. You don't need to acquire it from XML. This gives you the flexibility of dynamically assigning the Drawables it transitions between.
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);

ImageView not covering systemUI android

I am trying to have an ImageView of a screenshot take up the full screen (including the SystemUi nav bar at the top. I have tried the "Immersive full screen" introduced in Kitkat, and that does take away the UI bar, but my image still does not extend all the way to the top (as seen by the blue bar at the top of the image). The developer site says that it does this so that the image doesn't have to resize when the UI comes back, but it doesn't offer an alternate solution if you don't want the UI visible at all.
screenshot image <- I don't have enough reputation yet to post images.
Here is the code that I have tried (The commented out sections are other options that I have tried that didn't work either):
public class TrickScreen extends AppCompatActivity {
#Bind(R.id.screenshotImage) ImageView mScreenshotImage;
private Uri mMediaUri;
View mTrickScreenView;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trick_screen);
ButterKnife.bind(this);
mTrickScreenView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
int uiOptions2 = View.SYSTEM_UI_FLAG_IMMERSIVE;
int uiOptions3 = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
mTrickScreenView.setSystemUiVisibility(uiOptions | uiOptions2 | uiOptions3);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent intent = getIntent();
mMediaUri = intent.getData();
mScreenshotImage.setImageURI(mMediaUri);
// File file = new File(mMediaUri.getPath());
// Drawable drawable = Drawable.createFromPath(file.getAbsolutePath());
// rl = (RelativeLayout)findViewById(R.id.trickRelativeLayout);
// rl.setBackground(drawable);
I figured it out. Recently they have switched the XML format to include two XML docs per activity (e.g. activity_main.xml and content_main.xml). I usually only work in the content file, but you have to also ensure that the code in activity_main.xml is what you need. In my case I needed to remove one line - android:fitsSystemWindow = "true. Once I removed this line, the ImageView extends over the status bar and navigation bar areas.

setColorFilter does not work after setting background via setBackgroundResource

I crated a small app, which cycles through tinted images of an ImageView on clicks.
It works well with the image set in the layout file, but it does not work when setting the image from the code like below.
Any help appreciated.
public class MainActivity extends Activity {
private ImageView mPic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPic = (ImageView) findViewById(R.id.pic);
mPic.setBackgroundResource(R.drawable.msh);
mPic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random random=new Random();
ColorFilter cf = new PorterDuffColorFilter(Color.argb(192, random.nextInt(255), random.nextInt(255), random.nextInt(255)),Mode.SRC_ATOP);
mPic.setColorFilter(cf);
}
});
}
}
The color filter is applied to the ImageView content, not its background. Use ImageView#setImageResource(int resId) to set the content and color filter will be applied.
If you need to add the ColoFilter to ImageView's background, you can try something like mPic.getBackground().setColorFilter() (assuming that getBackground() returns non-null value).

How to add multiple custom views on an activity on a button click?

I am making an app in which an image is loaded from the gallery, now the user can add re-sizable and dragable rectangles on the image on the click of a button.
Right now i am able to add the image and also add a required rectangle on the image on the click of a button.
But what i want is that if the button is clicked again, a new rectangle is added, and the previous is restored as it is.
My code is as below..
public class ImageActivity extends Activity {
FrameLayout fm; //framelayout to add views on the image
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Bundle bundle = getIntent().getExtras(); //5 lines
final String value = bundle.getString("key"); //of code
Bitmap bmp = BitmapFactory.decodeFile(value); //to add
ImageView imageView = (ImageView) findViewById(R.id.imgView); //the image
imageView.setImageBitmap(bmp); //from gallery
Button bd = (Button)findViewById(R.id.buttonDraw); //button to add rectangle
fm = (FrameLayout)findViewById(R.id.main_view); //framelayout object
bd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
DrawPanel dview=new DrawPanel(ImageActivity.this);
fm.addView(dview);
}});
}
}
I am creating a new object of mycustom view for the rectangle each time the button is clicked. The problem here is that when the button is clicked for the first time, the rectangle is drawn with each of its functionalities.
But when i click the button again the previous one vanishes and no new rectangle is drawn.
There is no message in the console or in the log cat. What am i doing wrong...

Why does my animation freeze when returning to activity containing it?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).
This is my code:
public class CopyOfWorld extends Activity{
MediaPlayer muzak;
Boolean mSwitch = false;
ImageButton holmes;
AnimationDrawable holmesAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.world);
Preferencer pp = (Preferencer)getApplicationContext();
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
mSwitch = true;
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
}
public void clicker(View v){
Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
startActivityForResult(myIntent, 0);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
beginRender();
}
#Override
protected void onPause(){
super.onPause();
if(mSwitch){
muzak.release();
}
}
#Override
protected void onRestart(){
super.onRestart();
Preferencer pp = (Preferencer)getApplicationContext();
if(pp.getMuzak()){
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
beginRender();
}
public void beginRender(){
ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
StateListDrawable background = (StateListDrawable) holmes.getBackground();
Drawable current = background.getCurrent();
if(current instanceof AnimationDrawable){
holmesAnimation = (AnimationDrawable) current;
holmesAnimation.start();
}
}
}
I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.
Could anyone point me in the right direction?
EDIT:
I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)
Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.
Any clues?

Categories

Resources