I'm using the loadermanager for my app. On Orientation change my app can reload the TextView, but the imagestream for my drawable is not loaded the second time. On the first load the picture shows ,but after an orientation change it stays in its unchanged state. (in my case , a white square)
#Override
public void onLoadFinished(Loader<Person> loader, Person p) {
tvProfileName.setText(p.getName());
Log.i("img stream", ""+p.getImageStream());
Bitmap b = BitmapFactory.decodeStream(p.getImageStream());
Drawable d = new BitmapDrawable(getResources(), b);
ivMenuPhoto.setImageDrawable(d);
}
The setText is working and my name is loaded, even after the orientation change. But the setimagedrawable is not working. The p.getImageStream is not null...
Is my drawable maybe set before the layout is finished changing? I'm just very confused, because my TextView is working.
edited for my solution:
I added a Drawable attribute in the Person Object. So the loader can also hold that part. Then I created the getters and setters. And then I added the onResume to also set my drawable after the orientation change
#Override
public void onLoadFinished(Loader<Person> loader, Person p) {
tvProfileName.setText(p.getName());
Drawable d = new BitmapDrawable(getResources(), p.getPersonphoto());
this.personPhoto = d;
ivMenuPhoto.setImageDrawable(d);
}
#Override
protected void onResume() {
super.onResume();
ivMenuPhoto.setImageDrawable(this.personPhoto);
}
Related
I have got a Image (Bitmap) on a ImageView, without flickering. When I change something with setPixel(x, y, COLOR_VALUE), so some Pixels are changed on the ImageView, it begins to flicker, where I changed the Pixels.
public class Drawer extends ImageView {
private Bitmap someBitmap;
public void doSomeDrawing() {
for (int i = 0; i < 100; i = i + 2) {
someBitmap.setPixel(x, y, COLOR_VALUE);
}
setOnDraw();
}
public void setOnDraw() {
this.setImageBitmap(someBitmap);
}
Try getting a copy of your bitmap and draw on it. Then recycle your old bitmap.
The problem here might also be that setting the pixel takes time and if you do this on the UI Thread, it will slow down your app and may cause flickering too. How much time does doSomething take ?
i have a HashMap like this
HashMap<String, BitmapDrawable> bitmapDrawables = new HashMap<String, BitmapDrawable>();
Now i have a method which will return the drawable:
public static BitmapDrawable getDrawable(String fileName, Context context,Bitmap bitmap) {
BitmapDrawable drawable = bitmapDrawables.get(fileName);
if (drawable == null) {
drawable = convertBitmapToDrawable(bitmap, context);// will convert the bitmap to drawable
bitmapDrawables.put(fileName, drawable);
}
return drawable;
}
It works fine but some times it will not return the proper drawable, like i have 2 Activities A and B,in Activity A i have a button with drawable set, now i move from A -> B and then come back to A at that time the drawable is not loading, but when i touch that button the drawable is visible.
Why this is happening?, i have removed the haspMap and checked it its loads properly and when i move from A->B and come back to A,but if i dont use the Hashmap there is lag in the screens.
i am using the getDrawable() in this fashion:
button.setBackgroundDrawable(getDrawable(name,context,bitmap);
You can use
button.setBackgroundDrawable(getDrawable(name,context,bitmap)
in method onResume(). By the way, your implementation is not recommended, will cause memory leak easily. See this for more detail
In my Activity I need to change an ImageView background using a gradient, so I use an image with a transparent area, changing its background when I need. Here's some code:
private static View myImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivityLayout);
myImage = findViewById(R.id.myImageID);
}
[...]
private void myImageUpdate() {
GradientDrawable gradient;
int[] colors = {0xFF00FF00, 0xFF0000FF};
// I make some changes to these colors..
gradient = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors);
myImage.setBackgroundDrawable(gradient);
}
Now, the problem is:
If I call myImageUpdate() within onCreate() method, everything works fine.
If I call myImageUpdate() from another part of the code (like an onClick callback), I can't set my backgroud!
* UPDATE *
Guys, this code is fine... I was calling my method in a wrong (not directly reachable) line! My apologies...
I don't think this will fix it since you said myImageUpdate gets called within onClick... but try this..
runOnUiThread(new Runnable() {
public void run() {
myImage.setBackgroundDrawable(gradient);
}
});
you might have to make gradient variable final..
final GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors);
Try myImage.invalidate(). This will force the system to redraw the view.
Article: http://developer.android.com/resources/articles/avoiding-memory-leaks.html
metioned that:
private static Drawable sBackground;
#Override
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
label.setText("Leaks are bad");
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);
setContentView(label);
}
When a Drawable is attached to a view, the view is set as a callback
on the drawable.
That's true, drawable will hold that view as callback,
public void setBackgroundDrawable(Drawable d) {
......
d.setCallback(this);
But when screen orientation changes, drawable's callback will be reset as the new activity context. I think the previous textView will be detached from the static drawble, so that textview and according activity became recyclable.
So could anyone explain me in details?
Thanks.
I am facing a problem in my android app. I have to dynamically find the height of the text description coming from parsing the xml and show it on an imageview(background image).
I have to repeat the backround image by finding the total height of the text.
So, Please help me that how can we find the text height dynamically and repeat the background image according to that height.
Thanks.
Since there is no answer for this question.
I tried and almost did it in the following way:
public class MainActivity extends Activity
{
String description="hello how r u this project is meant for android to calculate " +
"the size of text and can dyanamically set the image height according to the text";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int length_count=description.length();
int window_width=getWindowManager().getDefaultDisplay().getWidth();
int lines=(length_count*14)/(window_width-30);
Log.v("length",""+length_count);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.project_dess_box_mid);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
LinearLayout layout = (LinearLayout)findViewById(R.id.linear1);
TextView tv=(TextView)findViewById(R.id.text01);
tv.setLines(lines);
tv.setText(description);
layout.setBackgroundDrawable(bitmapDrawable);
}
}
I posted this as it may be helpful for others too.