0

我目前正在构建一个需要使用 gif 的 android 应用程序,并且我有一个自定义GIFView来显示动画 gif。我想设置这个视图的大小,但是用像素写下大小不是很好,因为它在设备之间发生了巨大的变化。所以我认为将它们都设置为wrap_content可以解决这个问题,但事实并非如此。设置layout_widthwrap_content一切正常,但设置layout_heightwrap_content整个视图时消失。我该如何解决这个问题?

这是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();
        }
    }

}

我的 XML 文件:

            <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>

谢谢 :)

4

1 回答 1

-2

使用 Imageview 代替 view 并从 java 代码启动 GIFview 而不是将其设置为 xml。

于 2013-03-23T16:37:05.977 回答