Cannot scale gif move frame in Android Canvas

figma resize frame without resizing content
figma starting frame greyed out
figma can't select starting frame
figma resize to fit
figma frames
figma convert frame to group
figma clip content
figma artboard

I have problem with scaling GIF movie frame. I'm using Canvas.scale(float sx, float sy) before drawing the frame. But it doesn't draw scaled frame, it draws nothing. Any ides ?

If your trying to make a dedicated View that is scale-able and reusable within another layout you should use the View's getWidth() and getHeight() methods as well.

I ran into this issue when changing the View's dedicated size: canvas.getWidth() is the width of the entire screen and not the views width so I modified the onDraw method as follows.

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

Notice the getWidth(), getHeight() if statement checking to see if the movie is larger than the view, in my case my views height was 160 but my GIF was 260, however using the other method it would always draw the GIF too big, with these changes I was able to test and see it working in both cases where my View was 1280 X 900 and the image scaled up and also where my view was 1000 X 160 and it scaled down.

Best of luck, and have fun :D

-Chris

Frames in Figma – Figma, If you're designing for a specific device or screen size, you may want to create a Like artboards, frames allow you to choose an area of the canvas to create your canvas to create a default frame with 100 x 100 dimensions; Click and drag in a Solid Fill, Gradient, Images (PNG, JPEG, GIF, TIFF and WEBP) to a Frame. However, if you are still looking for some help, please post a message below so that members of the Canvas Community might be able to help you out. For now, since we've not heard from you and since there hasn't been any new activity in this thread for four months, I'm going to mark your question as "Assumed Answered".

This should work.

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();

}

How can I resize and move a gif? : GIMP, I have a gif that I am trying to turn into a bootscreen for my android tablet. able to resize the gif to a height of 480, but i need to make the width 800 and move it to New comments cannot be posted and votes cannot be cast image->canvas size. Is there a way to prevent that and substitute each frame by the next layer? The CanvasRenderingContext2D.scale() method of the Canvas 2D API adds a scaling transformation to the canvas units horizontally and/or vertically. By default, one unit on the canvas is exactly one pixel.

This was my solution:

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

add it to your layout-file:

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

then you can simply set the dimension in your Java code:

gifView.setSize(dimension);

An optimized implementation of GifDecoder for Android devices , Bitmap;. import android.util.Log;. import java.io. data necessary to decode the next frame in the animation sequence. File read status: Unable to open source. */. public static GIF Disposal Method meaning clear canvas to frame before last. */ protected int blockSize = 0; // block size last graphic control extension info. Duplicate design elements (Android) Move element (Android) Change element color (Android) Change element transparency (Android) Resize and rotate an element (Android) Layer elements (Android) Undo and redo changes (Android) Add frames (Android)

This sample allows us to resize GIF and fit in to the screen accordingly to the original size of image.

PlayGifView.class

public class PlayGifView extends View{

    private static final int DEFAULT_MOVIEW_DURATION = 1000;

    private int mMovieResourceId;
    private Movie mMovie;

    private long mMovieStart = 0;
    private int mCurrentAnimationTime = 0;
public Context context;
    @SuppressLint("NewApi")
    public PlayGifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
       /**
        * Starting from HONEYCOMB have to turn off HardWare acceleration to draw
        * Movie on Canvas.
        */
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
           setLayerType(View.LAYER_TYPE_SOFTWARE, null);
       }

   }

    public void setImageResource(int mvId){
        this.mMovieResourceId = mvId;
       mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
       requestLayout();
    }

    public void setMovieData(Movie movieImg){
        mMovie = movieImg;
        requestLayout();
    }

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   if(mMovie != null){

       //Find higest
       int exceptedSize = 200;
       int highest = 0;
       int quality = 0;
       if(mMovie.height()>mMovie.width()){
           highest=mMovie.height();
       }else{
           highest=mMovie.width();
       }

       //Get Display Size
       Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
       Point size = new Point();
       display.getSize(size);

       setMeasuredDimension((int)(((float)mMovie.width()/(float)highest)*((float) size.x))
               , (int)(((float)mMovie.height()/(float)highest)*((float)size.x)));
   }else{
       setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
   }
}

@Override
protected void onDraw(Canvas canvas) {
   if (mMovie != null){
       updateAnimtionTime();

       //Get Display Size
       Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
       Point size = new Point();
       display.getSize(size);

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

       canvas.scale(scale, scale);
       canvas.translate(((float) size.x / scale - (float) mMovie.width()) / 2f,
               0);

       drawGif(canvas);
       invalidate();
   }else{
//       drawGif(canvas);
   }
}

private void updateAnimtionTime() {
   long now = android.os.SystemClock.uptimeMillis();

   if (mMovieStart == 0) {
       mMovieStart = now;
   }
   int dur = mMovie.duration();
   if (dur == 0) {
       dur = DEFAULT_MOVIEW_DURATION;
   }
   mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}

private void drawGif(Canvas canvas) {
   mMovie.setTime(mCurrentAnimationTime);
   mMovie.draw(canvas, 0, 0);
   canvas.restore();
}

}

Main Activity.class

PlayGifView pGif = findViewById(R.id.viewGif);
pGif.setImageResource(R.drawable.ads1);

activity_main.xml

<com.---- Yours-----.PlayGifView
    android:id="@+id/viewGif"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

User guide Android/iOS, In a new project, the timeline will be only one frame long to begin with. You will see a Background layer (this cannot be deleted) and a default "Layer 1" with one drawing, Playback range - Adjust these sliders to play only part of the animation when previewing. Resize or crop/expand - Change the canvas size/ resolution. import android.graphics.Canvas; import android.graphics.Movie; import android.os.Build; import android.util.AttributeSet; import android.view.View; import com.basv.gifmoviewview.R; /** * This is a View class that wraps Android {@link Movie} object and displays it. * You can set GIF as a Movie object or as a resource id from XML or by calling

Resize and crop elements, Resize and crop elements, text, photos, and videos in your designs. Change element transparency (Android) � Resize and rotate an element (Android) Click any of the white circle handles on the corners, and drag in or out. You can also crop media added to frames and grids. note: You cannot crop GIFs and embeds. The PathInterpolator class is a new interpolator introduced in Android 5.0 (API 21). It is based on a Bézier curve or a Path object. This interpolator specifies a motion curve in a 1x1 square, with anchor points at (0,0) and (1,1) and control points as specified using the constructor arguments.

GIF Maker, UI Rendering is the act of generating a frame from your app and displaying it on the screen. If your jank cannot be reproduced on local devices, you can build custom performance This can work great, but it's also a lot of views moving around. RecyclerView or ListView: layout / draw taking too long. I am writing a android game using Canvas as the way to draw everything, the problem is that when i run it on different android phones the canvas dosn't change size i tried using canvas.scale() but that didn't make a i difference. The code i use for drawing is

Slow rendering, EdgeType � Canvas. You can control the format of this surface and, if you like, its size; the SurfaceView This means that translating and scaling a SurfaceView on screen will not cause rendering artifacts. A Drawable can call this to schedule the next frame of its animation. This value cannot be null . move_to(i) - Move to frame i of the gif; move_relative(i) - Move i frames ahead (or behind if i < 0) getters. get_canvas - The canvas element that the gif is playing in. Handy for assigning event handlers to. get_frames - The list of frames objects, containing an ImageData instance, a dataURL string, and a delay amount. frame object. data - an

Comments
  • You should draw draw gif first, then scale canvas.
  • I found that only using the 'else' statement in the drawing code yielded better results - ie. it scaled to ALL devices. I nixed the 'if' statement in my project.