Hook to get image after being loaded from a layout - android

Is there any way to get an image being pointed by an android layout and modify it before it is receceived by the requester?
For example if I have an image view like:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myimage.png"
/>
Is it possible to register to any hook (or any other way) where I could modify the image before the ImageView receives it?.
Trying to clarify a bit more the question:
1) The ussual way android works:
The app runs, and reads a layout with an imageview requesting #drawable/myimage.png
The image is loaded and provided to the imageview to show it
2) What I would like to try or accomplish:
The app runs, and reads a layout with an imageview requesting #drawable/myimage.png
I previously, somehow, told android to send me the image/stream before
providing it to the imageview. I modify it
I return the modified image and it is shown in the imageview.
Do anybody know if this is possilbe? I have seen offuscators like dexguard that can do this. Any tip would be really helpful because I haven't found any way to do it after some days of investigation.
Cheers.

You will need to extract bitmap from resources
Modify the bitmap
Apply modified bitmap to imageview through java code
These snippets might help
//extract bitmap from drawable resources
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon_resource);
//modify bitmap code here
//apply modified bitmap to image view
imageview.setBitmap(icon);

Related

Convert view into bitmap with modification

Requirement : User should be able to share the layout(which has image and text) as image.
Solution which i tried:
Bitmap b = Bitmap.createBitmap(myView.getWidth(), myView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
myView.draw(c);
This is working as expected. But I have few issues.
Issues:
I have few buttons and text which i don't want to convert as image. Also,I want to add my App logo as a watermark on the Bitmap image.
My solution for the above issue:
Before converting to Bitmap Image, Change visibility state of Button and Logo Image
Create one more layout behind the actual layout in a sharable image format. Use this layout to get Bitmap Image (not cool efficient, But easy way)
My question:
The above solutions are valid?
Or Is there any other method to do in an efficient way?
PS: I am not looking for code. :)
Issue 1: yes, you have to change visibility of those views you don't want them to be converted into image(the easiest way).
Issue 2: for adding a watermark, you can just draw it on canvas(you can use canvas.drawBitmap to do it) instead of putting it in the layout.

I want to enter images in my android activity. Where should I put the images?

I'm using Android Studio 1.2.2 I made a form activity and I want to insert an image. Where should I put the image? What changes I have to do in my mainactivity.java
file?
You can use Android's ImageView tag. You need to write it inside .xml file of your activity. You can take reference of it from android's devloper's website.
In Andorid studio you can not directly drag & drop images.
It will be good for you to take reference of any example.Here is the ImageView xml code.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
You can now make this ImageView reference in your MainActivity (Java) like below:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.imageView1);
Take a look at the docs: http://developer.android.com/guide/topics/resources/drawable-resource.html
Commonly, you will want to use some of the qualifiers by screen density and provide each image at different resolutions, docs here, this way lets Android pick up the best image size and scale it to match the phone screen.
Personally i use -nodpi and perform scalation by code, this way lets only have one copy of each image, and, gives you max control to position and size your images.
Hope this helps.

How to get the name of drawable through resource id?

I am trying to make the bitmap and for this I need the drawable name such as:
overlayScaledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.monkey);
This is working fine But my problem is that I can not do this same thing when a user selects a picture from a list because I have done this statically. As you can see I have wrote R.drawable.monkey. So its means every time it is going to create bitmap with monkey image.
But it is not the case, the user selects image which gets fixed into the image view. Now I want to get the ImageView as drawable. If it is possible then it could be dynamically and would be easy to handle. Any idea How I can get drawable of ImageView to use in bitmap scaling? please help
You could create an enum object containing all refs to the bitmaps and an id.
But another way is using getTag() and setTag() on the resources to identify by.

Android Peformance Optimization - How to draw a cached bitmap back to a textview

Basically, I want to cache a frequently drawn text view to increase the performance of my application.
So, on first-time draw, I cached the text view into a bitmap type buffer, using the methodology in these posts - get bitmap from textview in android & Converting a view to Bitmap without displaying it in Android?
Now, on subsequent draw requests, I want to draw the saved bitmap buffer back to a text view.
But, I am not able to find specimen code for that. There is a lot of discussion on SO for saving a bitmap from the textview, but not much regarding drawing the bitmap back to text view !
Please help me if anybody has worked in this area before.
You can convert the bitmap into a drawable object and then use the setBackroung method.
Bitmap mBitmap;
Drawable mDrawable=new new BitmapDrawable(mBitmap)
textView.setBackground(mDrawable);
But, his method is deprecated. You might save your bitmap into resources as android documention suggests.
The code you need is here:
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
When loading a bitmap into an ImageView, the LruCache is checked first. If an entry is found, it is used immediately to update the ImageView, otherwise a background thread is spawned to process the image: ....

How to listen for imageview is on screen?

I have a long list of icons (about 30) in my layout, so I am getting an OutOfMemory error in Android 2.3.3.
What I want to do is creating Drawable object if only necessary (icon is visible on the current scroll).
But how to now if imageview is on the screen or not? I couldn't find a listener for this.
My icons (png files) are already in drawables folder. There is no downloading process at all.
Try to use ImageLoader Library which provide you lots of custom option to load list with Images...enter link description here
This is how I solved my problem;
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.icon1);
Bitmap loBitmap = Bitmap.createScaledBitmap(image, iconSize , iconSize, true);
imageView.setImageBitmap(loBitmap);
iconSize was already calculated in my case.
I didn't want to use an external library for such simple task, so I give up with lazy loading and just get rid of OOM exception.
Because of my question is about listening for view entering the screen event, I will mark #Swap-IOS-Android's answer as the answer. You can check out the library source to see how it works.

Categories

Resources