我正在使用“圆形图像视图”放置轮廓图像。它在android studio的预览中显示良好,但在移动设备上检查时,圆形图像视图无法正常工作。问题的图像如下所示:
我尝试如下:
我的gradle依赖项:
implementation 'com.mikhaellopez:circularimageview:3.0.2'
我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5"
tools:context=".Fragment.AccountFragment">
<LinearLayout
android:id="@+id/profile"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/_3sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/profileImage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/_3sdp"
android:layout_weight="0.3"
android:src="@drawable/ic_dummy_user" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="3dp"
android:fontFamily="@string/register"
android:textColor="@color/black_text_color"
android:textSize="@dimen/_15sdp" />
<TextView
android:id="@+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@string/register"
android:textColor="@color/black_text_color"
android:textSize="@dimen/_15sdp" />
</LinearLayout>
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:fontFamily="@string/register"
android:textColor="@color/black_text_color"
android:textSize="@dimen/_15sdp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_20sdp"
android:layout_weight="2"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginTop="0.5dp"
android:background="@color/darkGray" />
<LinearLayout
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/_4sdp"
android:fontFamily="@string/register"
android:gravity="center_vertical"
android:text="@string/ride_summary"
android:textColor="@color/black_text_color"
android:textSize="@dimen/_15sdp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="0.5dp"
android:background="@color/darkGray" />
<LinearLayout
android:id="@+id/documents"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/_4sdp"
android:fontFamily="@string/register"
android:gravity="center_vertical"
android:text="@string/document"
android:textColor="@color/black_text_color"
android:textSize="@dimen/_15sdp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_marginTop="0.5dp"
android:background="@color/darkGray" />
<LinearLayout
android:id="@+id/payment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/_4sdp"
android:fontFamily="@string/register"
android:gravity="center_vertical"
android:text="@string/payment"
android:textColor="@color/black_text_color"
android:textSize="@dimen/_15sdp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:layout_marginTop="1dp"
android:background="@color/darkGray" />
</LinearLayout>
我将图像设置如下:
try {
URL url = new URL(image);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmapFrmUrl = BitmapFactory.decodeStream(input);
profileImage.setImageBitmap(bitmapFrmUrl);
} catch (IOException e) {
e.printStackTrace();
} }
答案 0 :(得分:2)
我个人喜欢使用de.hdodenhof插件,实现起来非常简单。只需将以下内容添加到gradle(模块:应用)实现'de.hdodenhof:circleimageview:3.0.0'中,然后将以下内容添加到您的xml
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profilePic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="start"
app:civ_border_width="2dp"
app:civ_border_color="#80ffffff"/>
答案 1 :(得分:1)
将Glide库用于圆形转换
RequestOptions options = new RequestOptions()
.centerCrop()
.apply(RequestOptions.circleCropTransform())
.placeholder(R.drawable.user)
.error(R.drawable.user);
Glide.with(context)
.load(user.getProfilePicUrl())
.apply(options)
.apply(RequestOptions.circleCropTransform())
.into(viewHolder.userImage);
答案 2 :(得分:0)
像这样使用圆形图像库。
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/profileImage"
android:layout_width="98dp"
android:layout_height="98dp"
android:layout_marginLeft="@dimen/_3sdp"
android:layout_weight="0.3"
android:src="@drawable/ic_dummy_user" />
答案 3 :(得分:0)
dependancies {
implementation 'de.hdodenhof:circleimageview:3.1.0'
}
然后在.xml中使用它:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profileImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:padding="3dp"
android:src="@drawable/ic_dummy_user"/>
如果它不起作用,请尝试此操作并再次检查:
Build-> Make Project