0

enter image description here my parent linear layout

        <LinearLayout
        android:id="@+id/gif_linear_layout"
        android:background="@drawable/shape"
        android:layout_marginTop="5dp"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical" />

so I have this class GifWebView

import android.content.Context;
import java.io.InputStream;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.os.SystemClock;
import android.view.View;

public class GifWebView extends View {
    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;

    public GifWebView(Context context, InputStream stream) {
        super(context);
        mStream = stream;
        mMovie = Movie.decodeStream(mStream);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();

        if (mMoviestart == 0) 
        {
            mMoviestart = now;
        }
        int gif_image_duration = mMovie.duration();
        if(gif_image_duration==0)
        {
            gif_image_duration = 1;
        }
        final int relTime = (int) ((now - mMoviestart) % gif_image_duration);
        mMovie.setTime(relTime);
        mMovie.draw(canvas, 10, 10);
        this.invalidate();
    }
}

i create instance of this class in my main activity to show gif images which workd perfectly in main activity

GifWebView gif_view;//global var

// already existing linear layout in my layout file 

// and then this in onCreate() method

gif_linear_layout = (LinearLayout)findViewById(R.id.gif_linear_layout);

InputStream stream = null;
        try {
            stream = getAssets().open(gif_name);
            //stream = getAssets().open("gif_images").;
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        gif_view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        gif_linear_layout.addView(gif_view);

now somebody please tell me how can set the layout parameters for the view created by creating the instance of the GifWebVIew class

actually i want the instance view to perfectly fit in to my linear layout but it is not fitting

any help ir suggestion please .

2 Answers2

1

You must use LinearLayout.LayoutParams, you do it correctly.

You should check:

  1. that the parent Layout has not a WRAP_CONTENT parameters. In that case, if the layout is not the main layout that must fill all the space, the internal View would not expand its size because the parent is wrapping around it.

  2. that the gif inside the View is correctly extended: is the image stretched when epanding the View size? I suggest you to put colored backgrounds to understand the exact bounds of the View and of the Layout, I fell into this problem so many times that I do it as a routine :)

  3. Seen that you correctly expand the image, it might well be that the image is not SCALED. To do this, you must use the ImageView capabilities to shrink and expand the image. Try to extend ImageView, instead of View, and then use setScaleType:

    public class GifWebView extends ImageView {

    // (...)

    gif_view.setScaleType(ScaleType.CENTER_INSIDE);

Beppi's
  • 2,089
  • 1
  • 21
  • 38
  • yeah please , anyways ,the linear layout is the parent element , shouldn't i focus on the child element to have LayOutParam as match_parent ? – Hussain Akhtar Wahid 'Ghouri' Sep 29 '13 at 20:11
  • I didn't complete the answer because I noticed that you correctly set the parameters, that should work correctly, the problem might be different. Let me check :) – Beppi's Sep 29 '13 at 20:12
  • the parent element doesn't has the wrap content attribute but has some definite value(200dp,200dp) , the GIF is not expanded , its just is not shown completely . i am adding a screenshot , please take a look – Hussain Akhtar Wahid 'Ghouri' Sep 29 '13 at 20:21
  • So the problem is that the gif is not shrinked to the size of the parent layout? – Beppi's Sep 29 '13 at 20:28
  • yes this is exactly the problem btw when is extend ImageView rather just View , the GIFs are not displayed – Hussain Akhtar Wahid 'Ghouri' Sep 29 '13 at 20:37
  • THis is more complex than what I thought. Did you see this, it may be what you are looking for. http://stackoverflow.com/questions/7373718/info-about-the-movie-class-for-android – Beppi's Sep 29 '13 at 20:43
  • :) :) :) , its ohk , anyways see the image i have added – Hussain Akhtar Wahid 'Ghouri' Sep 29 '13 at 20:43
  • I'm rather convinced that you must create a Bitmap and scale before writing it, as soon as ImageView does not provide you what you need and on the other side the scaling is not provided by the Movie class itself, so the solution of the link right before is exaclty what you need, I think. – Beppi's Sep 29 '13 at 20:47
1
// just replace your parent layout with this
 <LinearLayout
        android:id="@+id/gif_linear_layout"
        android:background="@drawable/shape"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67