3

I have developed an app that is suppose to display images located in the drawable folder. I used imageview/viewpager for it. However, I would like to display frame shown below.On the top of the image so that image appears more fancy.. Also, the frame should swipe along with the image...so that it looks more beautiful... I was thinking of creating it permanently on the image...through photoshop... But I didn't like that idea ..So I thought may be android have something for it....I am android beginner...So any code help along with explanation will be appreciated..Following are my codes..

Fancy frame - Ported from Op's link - See bottom of post

Mainactivity.java

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

public class MainActivity extends Activity {

    MediaPlayer oursong;
    ViewPager viewPager;
    ImageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
         oursong.seekTo(0);
         oursong.start();

         viewPager = (ViewPager) findViewById(R.id.view_pager);
         adapter = new ImageAdapter(this);
         viewPager.setAdapter(adapter);


         viewPager.setOnPageChangeListener(MyViewPagerListener);
    }



    private int pos = 0;
    @Override
    protected void onPause() {
            super.onPause();

           if(oursong != null){
               pos = oursong.getCurrentPosition();
               oursong.release();
               oursong = null;
            }
    }

    @Override
    protected void onResume(){
          super.onResume();

         oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
         oursong.seekTo(pos); // You will probably want to save an int to restore here
         oursong.start();
    }


   private final OnPageChangeListener MyViewPagerListener = new OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
              if (pos == adapter.getCount() - 1){
                 // adding null checks for safety
                 if(oursong != null){
                    oursong.pause();
                 }

               } else if (!oursong.isPlaying()){ 

                // adding null check for safety
                if(oursong != null){
                    oursong.start();
                }
              }         
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
             // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

            }
        };

 }

Imageadapter.java

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three,
        R.drawable.four,
        R.drawable.five
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.FIT_XY);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:icon="@drawable/icon" >



          <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
         android:icon="@drawable/icon" />
           <ImageView
        android:id="@+id/swipe_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/swipe_left" />

    <ImageView
        android:id="@+id/swipe_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/swipe_right" />

</RelativeLayout>

Edited

Hidden portion of the image under frame

Some portion of the image is hidden under the frame as indicated by white circles,black area was edited to maintain the privacy

biggboss2019
  • 220
  • 3
  • 8
  • 30

1 Answers1

3

You can do it using a LayerDrawable

A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index will be drawn on top.

You have two choices to use LayerDrawable.You can either define it in a separate drawable xml and then simply set the image in your ImageView, or you can configure a LayerDrawable dynamically in your code.

Programmatically using code

Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = r.getDrawable(R.drawable.yourImage);;
layers[1] = r.getDrawable(R.drawable.yourFrame);
LayerDrawable layerDrawable = new LayerDrawable(layers);
imageView.setImageDrawable(layerDrawable);

Now you have your ImageView having two images(1.your image and 2.Frame) set on it.

Edit :

In your ImageAdapter, you need to modify instantiateItem(ViewGroup container, int position) something like

@Override
public Object instantiateItem(ViewGroup container, int position) {
  ImageView imageView = new ImageView(context);
  int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
  imageView.setPadding(padding, padding, padding, padding);
  imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  Resources r = context.getResources();
  Drawable[] layers = new Drawable[2];
  layers[0] = r.getDrawable(GalImages[position]);
  layers[1] = r.getDrawable(R.drawable.yourFrame);
  LayerDrawable layerDrawable = new LayerDrawable(layers);
  imageView.setImageDrawable(layerDrawable);
  ((ViewPager) container).addView(imageView, 0);
  return imageView;
} 

Edit 2

As some portions of your image gets hidden under the frame, you need to set the width and height of your image before using it in the ImageView.Have some calculations of what could be the best width and height combination for your image, such that it will fit exactly with your frame.For setting height and width of your image

@Override
public Object instantiateItem(ViewGroup container, int position) {
  ImageView imageView = new ImageView(context);
  int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
  imageView.setPadding(padding, padding, padding, padding);
  imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  Resources r = context.getResources();
  Bitmap bmp = BitmapFactory.decodeResource(r, GalImages[position]);
  int width=200;//set your width
  int height=200;//set your height
  Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height, true);
  Drawable d = new BitmapDrawable(r,resizedbitmap);
  Drawable[] layers = new Drawable[2];
  layers[0] = d;
  layers[1] = r.getDrawable(R.drawable.yourFrame);
  LayerDrawable layerDrawable = new LayerDrawable(layers);
  imageView.setImageDrawable(layerDrawable);
  ((ViewPager) container).addView(imageView, 0);
  return imageView;
} 

Using XML

Create a new Drawable XML file, let's call it mylayer.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/yourimage" />
  <item android:drawable="@drawable/yourframe" />
</layer-list>

Now in your Activity set the image using that Drawable:

imageView.setImageDrawable(getResources().getDrawable(R.layout.mylayer));

I hope this gives you the basic idea for achieving what you want.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
  • @Sash_KP....thanks for reply....I was thinking of doing it in my java code.. can you please tell me where should I paste the above java code ? Also Do I have to do layers for each and every image ? – biggboss2019 Sep 10 '14 at 23:45
  • Please see my edit.Lemme know if it works for you.And i don't think you need to do it for each and every image.If you are able to see all the images(like R.drawable.one,two,three etc) right now, then this code should put the frame on top of every image automatically. – Sash_KP Sep 11 '14 at 00:11
  • @Sash_KP...thanks for reply..trying your code now..will let you know in couple of minutes.. – biggboss2019 Sep 11 '14 at 00:29
  • @Sash_KP..I tried your edited code...However, Now I can only see the frame with white background..and I cannot see my images .. – biggboss2019 Sep 11 '14 at 00:32
  • @Sash_KP...NVM...It did solve my problem...However, I ran into one more problem..and the problem is that my image appear to be cut under the frame..Is there a way so that my image fits the frame instead of cut... – biggboss2019 Sep 11 '14 at 00:39
  • Could you post a screenshot about how it looks and what exactly you mean by cut? – Sash_KP Sep 11 '14 at 09:18
  • @Sash_KP...please see my edited question, By cut I meant that some portion of the image is hidden under the frame as indicated by the white circles...and thanks for reply.. – biggboss2019 Sep 11 '14 at 14:54
  • Check **Edit 2** of my answer.Hope it works for you. – Sash_KP Sep 11 '14 at 16:03
  • @Sash_KP...Do I need to calculate the height and width of the frame or the image..Also is it possible to adjust the image under the frame on all devices ? – biggboss2019 Sep 11 '14 at 16:08
  • calculate of frame first.Then accordingly set a smaller width and height of your image so that it will fit into the frame.And if you use `dp` instead of `px`(like 200dp,250dp) then it will be device indendent.`px` is pixels but `dp` is device independent pixels. – Sash_KP Sep 11 '14 at 17:11
  • @Sash_KP..thanks for reply again...but that means that I have to resize all my images ...right ???...I thought there might be the code that will automatically fit my image under the frame... – biggboss2019 Sep 11 '14 at 17:15
  • No you don't have to resize all your images.Your images will be resized automatically using the code i suggested.Please consider upvoting the answer if it helped you. :) – Sash_KP Sep 11 '14 at 17:26
  • @Sash_KP..thanks for your help...But my frame size is 500 x 500 and I tried setting my width and height to 400 x 400 but It didn't work.. Any suggestion on how to fix it ...?. original width and height of the image is 1024 and 768 respectively.. – biggboss2019 Sep 11 '14 at 17:33
  • Did you set the width and height for your image to 400 each?If yes then note that `createScaledBitmap` Creates a new bitmap, scaled from an existing bitmap, when possible. If the specified width and height are the same as the current width and height of the source bitmap, the source bitmap is returned and no new bitmap is created.If this helps you solving your issue then try it.Else you can start a new question seeking help about `createScaledBitmap`. – Sash_KP Sep 11 '14 at 17:48
  • Okay, before you start a new question try to find out a solution here [in this SO question](http://stackoverflow.com/questions/2577221/android-how-to-create-runtime-thumbnail) – Sash_KP Sep 11 '14 at 18:07
  • @Sash_KP...Thanks for your link but I didn't found out the solution yet.. So if you wish you can follow this link http://stackoverflow.com/questions/25794570/original-image-gets-hidden-under-frame-in-viewpager-imageview ....to help me ...Thanks in advance... – biggboss2019 Sep 13 '14 at 15:22