How to get bitmap from ViewGroup? - android

I'm using recycler view where each item is simple ImageView. My RecyclerView is places in a ViewGroup - FrameLayout.
My task is to get from my FrameLayout - a bitmap.
That is, I need to get a bitmap that will contain pictures that are visible on the screen. That is, I need to draw my recycler on the bitmap, but not the entire recycler, but only the part that is visible on the screen
For example:
Is it possible to implement this? Please help me.

This code works for me and is appropriate for your use case :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
this is the code that gives visible items o recyclerView. It is very important to use rootView method
IntentUtils.prepareToShare(this,binding.recyclerView.rootView.drawToBitmap())
if you want to share your image use and see the result see this :
share image in android with intent

Pass your frameLayout to a function,
Bitmap result = loadBitmapFromView(frameLayout);
Following method will generate a view as a bitmap for you.
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}

Related

How can I change the color of my text based on the color of the ImageView it overlays?

So I've got transparant buttons with white text labels set up over a user uploaded ImageView. If the user uploads an image that is mostly white, then the buttons are hard to see if not completely invisible.
Does anyone know of a way to get the average color of a ImageView's source picture/drawable? If I can do this, I can compare it to a certain threshold I can trial and error for... If I can get this, then I can change the color of the text on my buttons to the inverted version of this color...or something??
Just idea spitting here..
And of course, if someone knows a better way, more info would be much appreciated, thanks!!
You could use the Palette class.
From the developers guide:
You can retrieve the prominent colors from the image using the getter
methods in the Palette class, such as Palette.getVibrantColor().
Palette.from() expects a Bitmap param, so you'll have to get it from your ImageView.
Something like this should work:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();
And then you can call the appropriate methods on this Palette instance to get the prominent colors, for example:
int darkVibrantColor = colorPalette.getDarkVibrantColor(someDefaultColor);
Check out this example screenshot on how the Palette class recognizes colors:
You can use Palette to get color of bitmap in an imageview
bitmap = BitmapFactory
.decodeResource(getResources(), R.drawable.header);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
//Set normal shade to textview
int vibrantColor = palette.getVibrantColor(R.color.primary_500);
//Set darkershade to textview
int vibrantDarkColor = palette
.getDarkVibrantColor(R.color.primary_700);
}
});
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_gravity="top|center"
android:background="#drawable/noimage"
android:scaleType="fitXY" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="Upload image"
android:textColor="#fff"
android:background="#34000000" />
</FrameLayout>
</LinearLayout>
You can set transparent color to button view . So button text is visible even if image is in white color. If its okay dont forget to thumb up answer!!

save multiple custom views as a single jpeg in android

In my app i got three custom views in single activity. So now i need to save it as a single jpeg. How to acheive this. I know how to save one or two bitmaps but this time its three custom views in single activity.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<com.wglxy.example.pinchzoompan.PanZoomView
android:id="#+id/zoomview1"
android:layout_width="100dp"
android:layout_height="700dp"
/>
<com.wglxy.example.pinchzoompan.PanZoomView1
android:id="#+id/zoomview2"
android:layout_width="100dp"
android:layout_height="700dp"
/>
<com.wglxy.example.pinchzoompan.PanZoomView2
android:id="#+id/zoomview3"
android:layout_width="100dp"
android:layout_height="700dp"
/>
</LinearLayout>
</FrameLayout>
each view has a picture, now i need to save it as a single jpeg. Can anyone suggest me some ideas.
Inflate your xml into single view by using layoutinflater then create a bitmap of that view.
i got the my three views saved as a single jpeg file with this code
layout = (LinearLayout) findViewById(R.id.viewLayout);
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
viewbitmap = layout.getDrawingCache(true);
and then
try {
viewbitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
just place all child in parent layout then setDrwaingCache true for that parent view after that whenever you need to take or save all child on that parent view you can getDrawingCache for that parent.

Custom menu item in ActionBarSherlock

I need to have a custom menu item in the ActionBar that just displays some custom formatted text. So I thought I would create an image on the fly and attach that to an menu icon. So in my code I thought I could use Layout xml to compose my text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:textColor="#bcbcbb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Some Text"/>
<TextView android:textColor="#ffffff"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/replace_text" android:text="XXXX"/>
</LinearLayout>
Then create a temp layout drawing that to a DrawableBitmap:
LinearLayout layout = (LinearLayout)View.inflate(getApplicationContext(), R.layout.refund_text, null );
layout.setDrawingCacheEnabled(true);
layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
layout.layout(0, 0, mRefundLayout.getWidth(), layout.getHeight());
layout.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(layout.getDrawingCache());
BitmapDrawable bm = new BitmapDrawable( getBaseContext().getResources(), bitmap);
layout.setDrawingCacheEnabled(false);
....do something with the bitmap
However after the measure call width and height are still zero. Can anyone tell me what I'm doing wrong?
Regards
Lee
If you only want to control the colours of the text in the MenuItem, you might be able to simply get away with formatting (using SpannableStringBuilder) the CharSequence you pass to MenuItem.add(int groupId, int itemId, int order, CharSequence title). I haven't tried that, let us know if it works!
If you ultimatately want to do more than that then create your own custom ActionProvider, inflating your layout in onCreateActionView() and "doing stuff" in onPerformDefaultAction().

FrameLayout borders in Android app

What I have to implement in order to limit the max size of a FrameLayout or the View inside it?
I am using a FrameLayout for my Android app:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/dog_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
I have added a View to it:
FrameLayout frame = (FrameLayout) findViewById(R.id.dog_holder);
dog = new DogView(this);
frame.addView(dog);
(DogView extends View)
I am initializing the View with a bitmap:
my_dog = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
I want to move around the picture (I have implemented the touch controls) but not to be able to move outside it. Good example is a picture gallery app: while a picture is zoomed you can go around it but on the borders there is bouncy effect which doesn't allow to go into the dark side.
What I have to implement to stop me when I try to go outside the dimension of the bitmap?

Android - canvas onDraw does not fire

I am trying to add a canvas view to a relative layout inside a horizontal scroll view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#fff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hsv"
android:layout_width="fill_parent"
android:background="#EEDB00"
android:layout_height="30mm">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/canvas"
android:background="#000"
android:layout_width="300mm"
android:layout_height="20mm">
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
I created a class called CanvasView which extends View, and I drew some basic shapes by over-riding onDraw(). However the canvas does not appear in the relativelayout, when I do:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cView = new CanvasView(this);
rLayout = (RelativeLayout)this.findViewById(R.id.canvas);
rLayout.addView(cView);
}
However, when I directly add it by calling setContentView(cView); it works. On digging, I found that when I call addView(), the onDraw() is not firing at all, and hence the canvas is not drawn... Any ideas on how to fix this?
I'm not in a position to test this myself at the moment, but I think your problem is that you're not applying any LayoutParams to the View, which may mean it occupies no screen space. If a View is ever off-screen or completely obscured or the system otherwise decides that nothing it draws will be visible, then I believe onDraw() won't be called at all.
Try setting some width and height to your View when you add it to your RelativeLayout:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50,50);
rLayout.addView(cView, params);

Categories

Resources