【发布时间】:2015-12-21 13:44:14
【问题描述】:
我一直在针对 Android 上的不同 API 测试我的应用。
我已经针对 API 17 - 22 对其进行了测试。
对于 API 17 和 18,我不明白,但 XML 布局在预览窗格中的显示似乎与模拟器上的不同。
我正在使用 Genymotion Emulator API 18 (768 X 1280) 和 API 17 (768 X 1280)。
这是图片在预览窗格中的样子:
这是一个完美的圆形图像视图,其XML如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerlistholder"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/group_container">
<com.example.simon.customshapes.CircularImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@drawable/circle"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:id="@+id/groupicon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="G"
android:id="@+id/group_letter"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:textSize="24sp"/>
</RelativeLayout>
<TextView
android:layout_toRightOf="@+id/group_container"
android:layout_toEndOf="@+id/group_container"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:id="@+id/name"
tools:text="Golf"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:id="@+id/group_desc"
tools:text="Sports"
android:layout_below="@+id/desc"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name" />
</RelativeLayout>
这是加载到 circularImageView 中的可绘制对象的 XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/primary"/>
<size android:width="80dp" android:height="80dp"/>
</shape>
CircularImageView 的代码可以在 google 上找到:
package com.example.simon.customshapes;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by Simon on 2015/07/25.
*/
public class CircularImageView extends ImageView {
public CircularImageView(Context context) {
super(context);
}
public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
//getting the padding adjustments
int w = getWidth(), h = getHeight();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(radius, radius,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
这是它在实际模拟器上的样子:
你可以看到圆圈被压扁了。
当我在模拟器 API 19 - 22 (1080 X 1920) 上运行此应用程序时,模拟器会显示圆形图像视图,因为它显示在预览窗格中。
起初我认为可能是屏幕尺寸问题,因为 API 19 - 22 在 1080 X 1920 上,而 API 17 - 18 在 768 X 1280 上,但后来我将 API 17 和 18 的模拟器大小更改为 1080 X 1920,它仍然显示相同的压扁圆圈。
API 17 - 18 不能正确显示我的圈子有什么原因吗?
【问题讨论】:
-
您在任何真实设备上看到过同样的问题吗?我也遇到过类似的奇怪问题,即模拟器运行失败,但它在真实设备上运行。
-
我实际上认为问题出在我已加载到圆形图像视图中的可绘制对象上。我将很快上传代码。 Unfort 我在 api 17 或 18 上没有任何实际设备
标签: android xml android-emulator android-custom-view genymotion