2016-07-17 59 views
0

我在與imageView的Android ImageView的小設備屏幕

這是更大的,當我打開它在小屏幕設備的問題上更大的

這個圖像顯示了不同 enter image description here

上其左側屏幕尺寸爲1440 * 2560 ,右側爲其尺寸400 * 800

這也是我的代碼xml ImageView

<RelativeLayout 

xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:id="@+id/tt"> 

<customfonts.RoundedImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/ssz2" 
android:scaleType="fitXY" 
android:adjustViewBounds="false" /> 

</RelativeLayout> 

,這是這讓我的形象四捨五入

public class RoundedImageView extends ImageView { 

public RoundedImageView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

public RoundedImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onDraw(Canvas canvas) { 

    Drawable drawable = getDrawable(); 

    if (drawable == null) { 
     return; 
    } 

    if (getWidth() == 0 || getHeight() == 0) { 
     return; 
    } 

    Bitmap b = null; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP 
      && drawable instanceof VectorDrawable) { 
     ((VectorDrawable) drawable).draw(canvas); 
     b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas c = new Canvas(); 
     c.setBitmap(b); 
     drawable.draw(c); 
    } 
    else { 
     b = ((BitmapDrawable) drawable).getBitmap(); 
    } 

    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

    int w = getWidth(), h = getHeight(); 

    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) 
     sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); 
    else 
     sbmp = bmp; 
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), 
      sbmp.getHeight(), 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, sbmp.getWidth(), sbmp.getHeight()); 

    paint.setAntiAlias(true); 
    paint.setFilterBitmap(true); 
    paint.setDither(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(Color.parseColor("#BAB399")); 
    canvas.drawCircle(sbmp.getWidth()/2+0.7f, sbmp.getHeight()/2+0.7f, 
      sbmp.getWidth()/2+0.1f, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(sbmp, rect, rect, paint); 


    return output; 
    } 
} 

和ImageView的源大小的類及其75 * 75

我要讓大小,如果這個ImageView的所有設備

相同

回答

1

您遇到的問題是,圖像中的像素大小相同。 但是由於屏幕右側的像素密度較低,因此看起來會更大。

您需要做的是將屏幕的像素和密度考慮在內。

這是Google使用dp來確保視圖看起來一樣,即使屏幕在設備之間不同也是如此。

解決此問題的方法有多種,但我認爲您希望將大小從像素轉換爲dp,然後再在畫布上繪製它。

+0

任何示例或教程都談論這個? – medo

0

您使用

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

如果你想固定的大小使用

android:layout_width="75dp" 
android:layout_height="75dp" 

,但我建議你不要。

瞭解更多關於像素密度獨立性https://developer.android.com/guide/practices/screens_support.html#density-independence

+0

'px'它使我的'ImageView'大屏幕上更小 – medo

+0

我的錯。使用dp(也稱dip爲密度獨立像素) – user345280

0

問題是您的ImageView尺寸設置爲「wrap_content」,並且您的內容(圖像源)尺寸是使用像素確定的。在屏幕較小,像素較少的設備上,這75個像素看起來會相對較大。

你應該設置你的ImageView有一個固定大小的DP如:

android:layout_width="50dp" 
android:layout_height="50dp" 

你可以使用計算器像這樣的:https://pixplicity.com/dp-px-converter/才能得到正確的密度像素尺寸。

+0

相同的沒有東西在圖像上改變了相同的結果 – medo

0

要支持所有屏幕設備,您必須創建具有不同尺寸的多個副本。

res/drawable-mdpi/graphic.png   // bitmap for medium-density 
res/drawable-hdpi/graphic.png   // bitmap for high-density 
res/drawable-xhdpi/graphic.png  // bitmap for extra-high-density 
res/drawable-xxhdpi/graphic.png  // bitmap for extra-extra-high-density 

See Android official docs.

+0

我知道這是,但我工作的聊天應用程序,所以這個圖像來自服務器 – medo