For example, if I add the following line to my 2nd activity, the alpha value for the background image will also change in my 1st activity, because I used the same image.
findViewById(R.id.main).getBackground().setAlpha(100);
But I don't want that. The two activities should have different alpha values, but still have the same drawable as background image. How can I achieve this?
You should call mutate() to create a separate instance before setting the alpha. As per the documentation:
A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.
Related
I want to modify the Leanback Video Player UI. There are two things I want to change:
The Seekbar in which I want to to be able to put the cue point markers for ads.
I want to change the positions of the buttons and the title and subtitle.
I tried extending the androidx.leanback.widget.SeekBar class and modifying it and overriding the lb_playback_transport_controls_row.xml file but that class has been marked as final. I already have the cue point locations and just need to modify the Seekbar.
I don't know which class to extend and modify and which layout file to override to put the poster in.
I have a class which encapsulates the information I want to pass over and it is parcelable. So I can pass its properties from one activity to another by using Intent.putExtra() method. But I cannot send images with it since images' sizes are larger than it should be (it doesn't matter whether they are Bitmap or ByteArray).
User downloads images from Firebase Storage and it is very likely that when user views an image, she won't see it again. So saving images in a cache-like storage isn't really necessary.
In Swift, I can do something like this:
// We are in currentViewController which is the activity that will send the image
let someNewViewController = NewViewController() // Create an instance of NewViewController class which is the activity that will receive image
someNewViewController.image = someImage // NewViewController class has a property called image, assign it an image from currentViewController
navigationController.pushViewController(someNewViewController, animated: True) // Go to someNewViewController activity
And then you can easily use image in the someNewViewController by accessing image property. When someNewViewController deallocated, image will be removed by garbage collection.
Is there a similar way in Kotlin/Java? Or is there an alternative to putExtra() that will allow large files to be transferred?
Thanks in advance!
I want to perform shared element transition when switching from Activity A to Activity B. The problem is that transition animation is not working smoothly when shared imageViews scale types are different. I am noticing a "bounce" in the very beginning of transition (on not animated imageView scaleType change I guess). The same "bounce" I am noticing when coming back from Activity B to Activity A.
Details:
Activity A contains imageView with scaleType: centerCrop.
Actvity B contains imageView with scaleType: fitXY.
Both ImageView have android:transitionName="sharedView", of course.
My transition set in xml looks like this:
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds/>
<changeImageTransform/>
</transitionSet>
In documentation about this config is written:
In combination with ChangeBounds, ChangeImageTransform allows
ImageViews that change size, shape, or ImageView.ScaleType to animate
contents smoothly.
So why my transition is not working smoothly? Or how to animate scaleType change during shared element transition if changeBounds with changeImageTransform doesn't seem to work?
Worth to mention that if I make scale types equal (e.g. centerCrop and centerCrop) of shared views - then transition works smoothly and everything is ok.
I've solved this problem in my case. Here is something you need to check:
Make sure your Shared element is ImageView for both source and target activity (Don't use any container).
Set the transitionName for the ImageView (not its container).
After I updated this, changeImageTransform will do its job changing scaleType smoothly between 2 ImageViews
Had same problem. Solved it by setting same padding property for both ImageView's. I don't know why it works so, but hope it can help someone.
In my app, I have a screen that displays some images next to text. These images are downloaded from the internet, so there will be some latency in displaying them. Right now I have an ImageView and a ProgressBar overlaying each other, and toggling the visibilities when the Bitmap becomes available. Is there any way to combine the two into one class that will handle it all in case I want to use this somewhere else?
One way to go about solving the problem is to make your own custom view which extends ProgressBar and draw the image in the ondraw(Canvasn canvas) using a Rect. That way, your image can be embedded in your view and you can always make it reusable by allowing your self to set the image via a setter/resource xml files which specify the attributes that go along with your custom view. Here's a reusable ProgressButton that I wrote that maybe of use to you, it shows how I did something similar to what I described (it's one which I think works well):
PregressButton
Yes there are a number of ways to combine widgets into a single custom class. Have a read of Custom Components. This should help you decide which approach you want to use for your situation.
In one app I'm developing I'm trying to programatically create an ImageButton that is a copy of the selected ImageButton, but the image is colorized in a different way, let's say red.
If I use the PowerDuff.Mode.MULTIPLY:
clonebutton.getDrawable().setColorFilter(0xFFFF0000,Mode.MULTIPLY);
Then even the original ImageButton changes its color to red since they share the same drawable. Is there a way to apply the filter only on the clonebutton without using two different drawables? For instance is it possible in some way to put a colorize layer on top of clonebutton without editing the drawable?
Update
I set the drawable as mutable:
Drawable d = swipebutton.getDrawable();
d.mutate();
d.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
swipebutton.setImageDrawable(d);
This prevents my clonebutton to share the state of its drawable to other views.
Drawable buttonBackground = clonebutton.getDrawable();
buttonBackground = buttonBackground.mutate();
buttonBackground.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
Make this drawable mutable. This operation cannot be reversed. A
mutable drawable is guaranteed to not share its state with any other
drawable. This is especially useful when you need to modify properties
of drawables loaded from resources. By default, all drawables
instances loaded from the same resource share a common state; if you
modify the state of one instance, all the other instances will receive
the same modification. Calling this method on a mutable Drawable will
have no effect.
On Lollipop you don't have to do this programmatic, i.e. colorfilters, at all if you don't want to. You can do it just by setting a tint on an xml drawable.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_back"
android:tint="#color/red_tint"/>
This might not work if you have an unlimiited number of colors but if they are limited this is a really good option. Check out my blog post for more information.
Drawable d=clonebutton.getDrawable()
d.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
clonebutton.setDrawable(d);
try this: or take the below code as per your need
switch(v.getId())
{
case R.id.bt1:
Drawable d=b11.getBackground();
d.setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
b11.setBackgroundDrawable(d);
b12.setBackgroundResource(android.R.drawable.btn_default);
break;
case R.id.bt2:
//b2.getBackground().setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
Drawable dd=b12.getBackground();
dd.setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
b12.setBackgroundDrawable(dd);
b11.setBackgroundResource(android.R.drawable.btn_default);
break;
}
switching color of buttons after onclick