我在Fresco中遇到圆形图像问题

时间:2016-11-23 17:45:00

标签: android fresco

我有圆形图像的问题,不起作用,Fresco下载图像并显示但不能转换为圆形。我不知道我错过了这是我的代码。

Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"
    defaultConfig {
    applicationId "pruebas.imaginamos.com.pruebas"
    minSdkVersion 21
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.facebook.fresco:fresco:0.14.1'
    testCompile 'junit:junit:4.12'
   //compile 'com.mostafagazar:customshapeimageview:1.0.4'
   // If your app supports Android versions before Ice Cream Sandwich (API level 14)
   compile 'com.facebook.fresco:animated-base-support:0.14.1'

}

这是MainActivity5.java:

package pruebas.imaginamos.com.pruebas;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.facebook.drawee.view.SimpleDraweeView;

public class MainActivity5 extends AppCompatActivity {

   SimpleDraweeView simpleDraweeView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        String url = "https://firebasestorage.googleapis.com/v0/b/farmatodo-dev.appspot.com/o/imChat%2F2e4120c7-3755-4537-aceb-c2ed9d97ad01%2F1479745196209-about-06.jpg?alt=media&token=fb0c3c8a-eeac-488b-96d2-51bc7cbd088a";

        Uri uri = Uri.parse(url);


        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.avatarImageView);
        simpleDraweeView.setImageURI(uri);


}

}

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fresco="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pruebas.imaginamos.com.pruebas.MainActivity5">


<RelativeLayout
    android:layout_width="match_parent"
    android:background="@color/colorAccentLight"
    android:layout_height="wrap_content">
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/avatarImageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        fresco:placeholderImageScaleType="centerCrop"
        fresco:placeholderImage="@drawable/photo_female_3"
        fresco:roundAsCircle="true"/>
</RelativeLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:14)

替换此行,

xmlns:fresco="http://schemas.android.com/tools"

在xml文件中使用此行

xmlns:fresco="http://schemas.android.com/apk/res-auto"

答案 1 :(得分:0)

您可以查看此答案的圆圈和带边框的圆圈

public static void loadImageCircle(String url, SimpleDraweeView targetView){
    ImageRequest request = 
    ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
        .setProgressiveRenderingEnabled(false)
        .build();
    DraweeController controller = Fresco.newDraweeControllerBuilder()
        .setImageRequest(request)
        .setAutoPlayAnimations(false)
        .build();

    RoundingParams roundingParams = new RoundingParams();
    roundingParams.setRoundAsCircle(true);
    targetView.getHierarchy().setRoundingParams(roundingParams);

    targetView.setController(controller);
}

https://shareyourknowledge2017.wordpress.com/2017/07/31/android-fresco-displayingcircleandcirclewithborder/

答案 2 :(得分:0)

替代解决方案: -

使用自定义drawable并将其设置为imageview background shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="oval">
              <solid android:color="@color/gray"/>
                <!--shadow Color-->
        </shape>
    </item>

    <item
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="3dp">
        <shape android:shape="oval">
             <solid android:color="@color/lightgrey"/>//your background color here
        </shape>
    </item>

像这样使用它: -

android:background="@drawable/shadow"

您可以尝试另一种解决方案: - https://github.com/Pkmmte/CircularImageView

此库可用于您的要求