Android GIF渲染问题

时间:2015-12-25 22:06:56

标签: android android-layout android-studio gif animated-gif

在我的activity_main.xml文件中,我有以下代码块,因为我想使用它来为我的GIF文件设置动画。

func longPressAction(gestureRecognizer: UIGestureRecognizer) {

    if (gestureRecognizer.state == UIGestureRecognizerState.Began) {
        let touchPoint = gestureRecognizer.locationInView(self.mapView)
        let annotation = MKPointAnnotation()
        let newCoords = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
        let location = CLLocation(latitude: newCoords.latitude, longitude: newCoords.longitude)

        CLGeocoder().reverseGeocodeLocation(location) { [weak self] (placemarks, error) -> Void in
            if let strongSelf = self{
                if (error != nil) {
                } else {
                    if let p = placemarks?[0] {
                        var address:String = ""

                        if (p.subThoroughfare != nil) {
                            address = p.subThoroughfare!
                        } else if (p.subThoroughfare == nil && p.thoroughfare != nil) {
                            address = p.thoroughfare!
                        } else if (p.subThoroughfare == nil && p.thoroughfare == nil && p.country != nil) {
                            address = p.country!
                        } else if (p.subThoroughfare == nil && p.thoroughfare == nil && p.country == nil) {
                            address = "New Place"
                        }

                        print(address)
                        annotation.title = address
                        annotation.coordinate = newCoords
                        strongSelf.mapView.addAnnotation(annotation)
                        strongSelf.sharedAnnotations.addObject(annotation)
                    }
                }
            }
        }
    }

GIF的动画效果很好,但这会导致Android Studio中出现渲染问题。有没有解决这个问题?

PlayGifView的代码如下:

<matthewallenlinsoftware.keepy_uppy.PlayGifView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/viewGif"
    android:layout_gravity="center"
    android:layout_weight="0.04"
    android:src="@drawable/bouncing_ball" />

2 个答案:

答案 0 :(得分:0)

如果您将GIF保留在hdpi,xhdpi等文件夹中。直接复制GIF&#39; drawable&#39;也或尝试以管理员身份运行android studio ...

答案 1 :(得分:0)

如果使用不同的库没有问题,那么你可以试试这个

Droids on Roids

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.22'

和顶级build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
}
allprojects {
    repositories {
        mavenCentral()
    }
}

在您看来 XML

<pl.droidsonroids.gif.GifImageView 
    android:layout_width=”match_parent”  
    android:layout_height=”match_parent” 
    android:src=”@drawable/src_anim” 
    android:background=”@drawable/bg_anim”/>

如果您需要在线下载功能,那么您可以尝试Glide

repositories {
  google()
  mavenCentral()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.12.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

然后从 URL 加载

Glide.with(this).load(“your_gif.gif") .into(imageView); 

并从资源加载

Glide.with(this).load(R.raw.your_gif).into(imageView);