GIF图像问题

时间:2016-01-21 08:12:59

标签: android

所以我在我的应用中使用了GIF。但是,当我在其他设备上安装该应用时,GIF不适合屏幕,或者它不在应有的位置。有人有解决方案吗?

2 个答案:

答案 0 :(得分:0)

类别:

 package com.example.cyril.tryapp;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Movie;
    import android.util.AttributeSet;
    import android.view.View;

import java.io.InputStream;

public class GifView extends View {

    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth = 150, movieHeight = 500;
    private long movieDuration;
    private long mMovieStart;

    public GifView(Context context) {
        super(context);
        init(context);
    }

    public GifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GifView(Context context, AttributeSet attrs,
                   int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        setFocusable(true);
        gifInputStream = context.getResources()
                .openRawResource(R.drawable.maestragif);

        gifMovie = Movie.decodeStream(gifInputStream);
        movieWidth = gifMovie.width();
        movieHeight = gifMovie.height();
        movieDuration = gifMovie.duration();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, 
            int heightMeasureSpec) {
        setMeasuredDimension(movieWidth, movieHeight);
    }

    public int getMovieWidth(){
        return movieWidth;
    }

    public int getMovieHeight(){
        return movieHeight;
    }

    public long getMovieDuration(){
        return movieDuration;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) {   // first time
            mMovieStart = now;
        }

        if (gifMovie != null) {

            int dur = gifMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }

            int relTime = (int)((now - mMovieStart) % dur);

            gifMovie.setTime(relTime);

            gifMovie.draw(canvas, 0, 0);
            invalidate();

        }

    }

}

XML:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_mainplain"
    android:layout_gravity="center">



<com.example.cyril.tryapp.GifView
        android:id="@+id/gifview"
        android:layout_width="130dp"
        android:layout_height="230dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="30dp" />

    <com.example.cyril.tryapp.GifView2
        android:id="@+id/gifview2"
        android:layout_width="350dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

答案 1 :(得分:0)

您的gif不适合所有屏幕,因为您使用特定值设置width和heignt属性:

android:layout_width="130dp"
android:layout_height="230dp"

DP值与密度无关,但它们在每个屏幕上的大小不同,应用比例因子将DP值转换为PX值