0

I am currently building an android application that requires the use of gifs, and I have a custom GIFView to display animated gifs. I wanted to set the size of this view, but writing down the size with pixels is not so good, because it changes dramatically from device to device. So I thought that setting them both to wrap_content would solve this problem, but it doesn't. When setting the layout_width to wrap_content everything is ok, but when setting the layout_height to wrap_content to whole view just disappears. How can i solve this?

This is the GIFView:

package XXX.XXX.XXX;

import java.io.InputStream;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;

public class GIFView extends View {

    private Movie movie;
    private int gifId;
    private long movieStart;

    public GIFView(Context context) {
        super(context);
        initializeView();
    }

    public GIFView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAttrs(attrs);
        initializeView();
    }

    public GIFView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setAttrs(attrs);
        initializeView();
    }

    public void setGIFResource(int resId) {
        this.gifId = resId;
        initializeView();
    }

    public int getGIFResource() {
        return this.gifId;
    }

    private void initializeView() {
        if (gifId != 0) {
            InputStream is = getContext().getResources().openRawResource(gifId);
            movie = Movie.decodeStream(is);
            movieStart = 0;
            this.invalidate();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        if (movieStart == 0) {
            movieStart = now;
        }
        if (movie != null) {
            int relTime = (int) ((now - movieStart) % movie.duration());
            movie.setTime(relTime);
            movie.draw(canvas, getWidth() - movie.width(), getHeight() - movie.height());
            this.invalidate();
        }
    }

    private void setAttrs(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GIFView, 0, 0);
            String gifSource = a.getString(R.styleable.GIFView_src);
            //little workaround here. Who knows better approach on how to easily get resource id - please share
            String sourceName = Uri.parse(gifSource).getLastPathSegment().replace(".gif", "");
            setGIFResource(getResources().getIdentifier(sourceName, "drawable", getContext().getPackageName()));
            a.recycle();
        }
    }

}

My XML file:

            <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/course_title"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:padding="5dp"
            android:background="#A47D42">

            <XXX.XXX.XXX.GIFView
                android:id="@+id/animation_1"
                android:layout_width="240px"
                android:layout_height="200px"
                android:layout_gravity="center_horizontal"
                app:src="@drawable/someanimation" >
            </XXX.XXX.XXX.GIFView>
        </LinearLayout>

Thanks :)

rel-s
  • 6,108
  • 11
  • 38
  • 50

1 Answers1

-2

Use Imageview in place of view and initate GIFview from java code rather than set it xml.

Koushik
  • 345
  • 1
  • 7