Android: Image over image - android

I have a video thumbnail and when you click on it, the video starts playing in the YouTube player.
This works, but It's not clear that you have to click on the thumbnail to play, therefore, I want to draw a play button image over the thumbnail in the bottom right corner. How would I go about this?
I currently have the thumbnail in a Drawable object and the play button in my drawable resource directory.
I tried stuff with bitmaps and canvas but it's quite hard and I don't know if this is the way to go.

Use Relative or FrameLayout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/thumbnail"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
or
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/thumbnail"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_gravity="bottom|right"/>
</FrameLayout>

What about using a FrameLayout or a RelativeLayout to stack the views..
Here follows some psaudo code:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.yourpackage.VideoPlayer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></VideoPlayer>
<!-- Adjust the layout position of the button with gravity, margins etc... -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Play"
/>
</FrameLayout>

I used RelativeLayout and it worked for me
<RelativeLayout
android:id="#+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:clickable="true"
android:layout_gravity="top"
android:maxHeight="400dp"/>
<ImageView
android:id="#+id/play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_menu_play_large"
android:layout_centerInParent="true" />
</RelativeLayout>

This is Easiest Way i Found i think to merge morethan one images in one. this is very helpful if we want share merged file.
Resources r = getResources();
Drawable[] layers = new Drawable[3];
layers[0] = r.getDrawable(R.drawable.image3);
layers[1] = r.getDrawable(R.drawable.image1);
layers[2] = r.getDrawable(R.drawable.image2);
LayerDrawable layerDrawable= new LayerDrawable(layers);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// align image1 over image3
layerDrawable.setLayerGravity(1, Gravity.TOP);
layerDrawable.setLayerInsetTop(1, 250);
// align image2 over image3
layerDrawable.setLayerGravity(2, Gravity.BOTTOM);
layerDrawable.setLayerInsetBottom(2, 250);
}
// both image1 & 2 placed over image3 in layerDrawable.
imageView.setImageDrawable(layerDrawable);
Hope this will works

Related

How to make two images one above each other in android

In My application I have one big imageview 'A' and six small imageviews s1,s2,s3,s4,s5,s6,intially set the all imageviews with some drawbles.
when drag the small images(s1,s2 etc) and drop on the big imageview 'A' then it is replacing with corresponding image.
for example lets say Imageview A have BAT Image and imageview s1 have the BALL Image when drag the BALL image onto BAT Image then the BAT Image is replacing with BALL Image.
MY Requirement is that when i drag the BALL Image onto the BAT Image then the BAT image should be there and the BALL Image should be overlap(one above each other).so BAT and BALL images should be there on imageview A.
You can use FrameLayout or RelativeLayout. The order of the imageviews will be lastly inserted will be shown above.
eg:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bat"
/>
<ImageView
android:id="#+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball"
/>
</RelativeLayout>
Use below xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/RelativeLayout1"
android:orientation="vertical">
<!-- Add image A -->
<ImageView android:id="#+id/image1"
android:layout_width="800dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
android:src="#drawable/image1" />
<!-- Add smaller image-->
<ImageView android:id="#+id/image2"
android:layout_width="400dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_alignTop="#+id/image1"
android:src="#drawable/image2" />
</RelativeLayout>
The output will look like below
Use FrameLayout.
For example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#drawable/prescription_not_attached"
android:layout_gravity="center" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#drawable/home_measure"
android:layout_gravity="center" />
</FrameLayout>
Replace my Images with yours.
Hope it helps, Thank you
Try using a FrameLayout, using Linear will not allow overlapping...
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
ImageView bat = new ImageView(this);
iv.setImageResource(R.drawable.bat);
ImageView ball = new ImageView(this);
i.setImageResource(R.drawable.ball);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
i.setLayoutParams(layoutParams);
frame.addView(bat);
frame.addView(ball);

How to position image1 on right top of the image2 in android?

How can i achieve a layout with 2 images in android, one image overlaying another image at the top right?
an image overlapping another image at the top right
http://i.imgur.com/uT3L2wl.jpg
I tried using relative layout, but i could position the overlay image by giving layout margin, but if i do that i cant support all screen sizes with that design.
and also i couldnt position the overlay image with respect to the underimage.
i want to design my layout like the pic i have shared.
I could get this working from the below code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageButton
android:id="#+id/addnewcustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/c1"
android:layout_toRightOf="#id/c1" >
</RelativeLayout>
<ImageButton
android:id="#+id/hover1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/c1"
android:layout_alignLeft="#+id/c1"
android:layout_marginBottom="23dp"
android:layout_marginLeft="22dp"
android:background="#null"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Try look at FrameLayout. It will do the job.
http://www.learn-android-easily.com/2013/05/frame-layout-in-androis.html

Android Designing Layout programmatically

How can I make this layout in android programmatically.
I want just an idea not whole coding thing.
Here is the answer but not the direct answer. This is what I personally do to create complex layout programatically.
Create the same layout in XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/some_big_image" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#DD000000" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView"
android:textColor="#android:color/white" />
</RelativeLayout>
Now start from top parent to child.
RelativeLayout mParent = new RelativeLayout(this);
RelativeLayout.LayoutParams mParentParams = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
mParent.setLayoutParams(mParentParams);
ImageView mBigImageView = new ImageView(this);
mBigImageView.setLayoutParams(mParentParams);
mParent.addView(mBigImageView);
As you practice you can directly code the same without creating xml.
Basic Idea:
Create a RelativeLayout which contains one ImageView and two TextView which is aligned bottom
set the alpha of the RelativeLayout to adjust the transparency
You can have one relative layout which contain one image view for big image and below it one more linear layout containing image and text as "pearl Continental ..." and background of liner layout should be 50-60% transparent.
Create the background for LL using shapes. and set it as align bottom of parent (relative) layout.
Hope this helps you

Android mask a bitmap with a transparent png

I have a png image with some transparent areas (like gradient-transparent) and I'd like to "add" this image to over a Bitmap and create a new Bitmap from these two.
is this possible in Android?
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="sliding banner here" />
<ImageView
android:id="#+id/slider_des"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginTop="40dp"
android:hint="text"
android:paddingBottom="20dp"
/>
</FrameLayout>
The above code can be modified to achieve what you want! the first imageview would be for your bitmap and 2nd for overlapping image view that you can placeyour transparent png in.

WebView doesn't align top or bottom in its container

I have a strange problem.
I have an ImageView and on this ImageView is a WebView. This WebView ist as big as the ImageView an its background is transparent so that only the text is displayed by the WebView.
On my Galaxy Nexus this works perfectly.
But on my Sony Xperia, the WebView doesn't align the top and the bottom of the image. Left an Right alignment is O.K.
But why is this like this?
Here is my xml-code:
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_centerHorizontal="true"
android:src="#drawable/product_pic_background" />
<RelativeLayout
android:id="#+id/contain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/ImageView02"
android:layout_alignLeft="#+id/ImageView02"
android:layout_alignRight="#+id/ImageView02"
android:layout_alignTop="#+id/ImageView02"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="50dp" >
<WebView
android:id="#+id/produkttext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_alignParentStart="true"
android:longClickable="false"
android:scrollbars="vertical" />
</RelativeLayout>
1.Xperia arc. how it shouldn't be
2.Galaxy Nexus. how it should be
Ok, one solution to this, once we have the requirements:
<RelativeLayout
android:id="#+id/contain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="15dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:paddingTop="50dip" >
<WebView
android:id="#+id/produkttext"
android:longClickable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#+drawable/product_pic_background"
android:scrollbars="vertical" />
</RelativeLayout>
Then in the code:
WebView web = (WebView) findViewById(R.id.produkttext);
web.setBackgroundColor(0);
BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.product_pic_background);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = bd.getBitmap().getHeight();
rl.getLayoutParams().width = bd.getBitmap().getWidth();
This will make the WebView transparent, so to see the background image, as well as to match the parent layout by width and height.
Then, the RelativeLayout will be set with the dimensions of the picture's drawable.
I haven't manage to test it (I can't at the moment), but if this doesn't work, I will delete this comment (and will possibly commit harakiri :-)).

Categories

Resources