2

GIF ムービー フレームのスケーリングに問題があります。フレームを描画する前に Canvas.scale(float sx, float sy) を使用しています。ただし、スケーリングされたフレームは描画されず、何も描画されません。アイデアはありますか?

4

4 に答える 4

3

別のレイアウト内でスケーリング可能で再利用可能な専用のビューを作成しようとしている場合は、ビューの getWidth() および getHeight() メソッドも使用する必要があります。

ビューの専用サイズを変更すると、この問題に遭遇しました。 canvas.getWidth() はビューの幅ではなく画面全体の幅であるため、 onDraw メソッドを次のように変更しました。

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);

    long now = android.os.SystemClock.uptimeMillis();  

    Paint p = new Paint();
    p.setAntiAlias(true);

    if (cur == 0) cur = now;  

    if(movie != null)
    {
        float scale = 1f;
        if(movie.height() > getHeight() || movie.width() > getWidth())
            scale = ( 1f / Math.min(canvas.getHeight() / movie.height(), canvas.getWidth() / movie.width()) ) + 0.25f;
        else    
            scale = Math.min(canvas.getHeight() / movie.height(), canvas.getWidth() / movie.width());

        canvas.scale(scale, scale);
        canvas.translate(((float)getWidth() / scale - (float)movie.width())/2f,
                ((float)getHeight() / scale - (float)movie.height())/2f);

        int relTime = (int)((now - cur) % movie.duration());
        movie.setTime(relTime);
        movie.draw(canvas, 0, 0);


        invalidate();
    }
}

ムービーがビューよりも大きいかどうかをチェックする getWidth()、getHeight() if ステートメントに注意してください。私の場合、ビューの高さは 160 でしたが、GIF は 260 でしたが、他の方法を使用すると常に GIF が大きすぎて描画されます。 、これらの変更により、ビューが 1280 X 900 で画像が拡大された場合と、ビューが 1000 X 160 で縮小された場合の両方でテストして動作することを確認できました。

頑張って、楽しんでください :D

-クリス

于 2014-05-03T09:44:35.527 に答える
2

これはうまくいくはずです。

protected void onDraw(Canvas canvas)
{
    canvas.drawColor(Color.TRANSPARENT);

    canvas.scale(0.5f, 0.5f); // scale gif to its half size

    super.onDraw(canvas);

    /* movie.setTime . . . */

    movie.draw(canvas, ...);

    this.invalidate();

}
于 2012-12-15T19:09:20.273 に答える
1

これが私の解決策でした:

public class GifImageView extends View {
    private InputStream mInputStream;
    private Movie mMovie;
    public int mWidth, mHeight;
    private long mStart;
    private Context mContext;

    public GifImageView(Context context) {
        super(context);
        this.mContext = context;
    }

    public GifImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GifImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        if (attrs.getAttributeName(1).equals("background")) {
            int id = Integer.parseInt(attrs.getAttributeValue(1).substring(1));
            setGifImageResource(id);
        }
    }

    private void init() {
        setFocusable(true);
        mMovie = Movie.decodeStream(mInputStream);
        mWidth = mMovie.width();
        mHeight = mMovie.height();

        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        long now = SystemClock.uptimeMillis();

        if (mStart == 0) {
            mStart = now;
        }

        if (mMovie != null) {
            int duration = mMovie.duration();
            if (duration == 0) {
                duration = 1000;
            }

            float scale = Math.min((float) canvas.getHeight() / (float) mMovie.height(), (float) canvas.getWidth() / (float) mMovie.width());

            canvas.scale(scale, scale);
            canvas.translate(((float) mWidth / scale - (float) mMovie.width()) / 2f,
                ((float) mHeight / scale - (float) mMovie.height()) / 2f);

            int relTime = (int) ((now - mStart) % duration);

            mMovie.setTime(relTime);
            mMovie.draw(canvas, 0, 0);
            invalidate();
        }
    }

    public void setGifImageResource(int id) {
        mInputStream = mContext.getResources().openRawResource(id);
        init();
    }

    public void setGifImageUri(Uri uri) {
        try {
            mInputStream = mContext.getContentResolver().openInputStream(uri);
            init();
        } catch (FileNotFoundException e) {
            Log.e("GIfImageView", "File not found");
        }
    }

    public void setSize(int dimension) {
        mWidth = dimension;
        mHeight = dimension;
    }
}

レイアウト ファイルに追加します。

<GifImageView
        android:id="@+id/gifView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/image" />

次に、Java コードでディメンションを設定するだけです。

gifView.setSize(dimension);
于 2018-08-24T07:34:17.670 に答える