So I am using a GIF in my app . But when I install the app in other device the GIF doesn't fit in the screen or it's not on the place it should be. Anyone have solution for this ?
Asked
Active
Viewed 41 times
2 Answers
0
Class:
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>

Cyril
- 9
- 5
0
Your gif will not fit all screens because you set the width and heignt attributes with a specific value:
android:layout_width="130dp"
android:layout_height="230dp"
DP values are density independent, but they're not the same size on every screens, a scaling factor is applied to convert the DP value to a PX value

Nicolas Cortell
- 659
- 4
- 16
-
-
You want to fit the whole screen with the image or you want some margins? – Nicolas Cortell Jan 21 '16 at 12:42
-
no not the whole screen. I want the first gif in the left side of the screen and the other in the top part . – Cyril Jan 21 '16 at 12:45
-
You can use the `weight` attribute: http://www.chess-ix.com/blog/the-use-of-layout_weight-with-android-layouts/ and you can use the `gravity` / `layout_gravity` : http://stackoverflow.com/a/3482757/5778152 – Nicolas Cortell Jan 21 '16 at 13:09