2016-07-17 3 views
0

私はimageViewAndroidの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

は 'その大画面で私の' ImageView'より小さく – medo

+0

私のミスを犯すpx'しました。 dp(密度非依存ピクセルとしてdipとも呼ばれます)を使用してください – user345280

0

問題は、ImageViewの寸法が "wrap_content"に設定され、コンテンツ(画像ソース)のサイズがピクセルで決定されることです。ピクセルの少ない画面が小さいデバイスでは、75ピクセルは比較的大きく見えます。

次のような固定されたDPの大きさを持っているあなたのImageViewのを設定する必要があります。正しい密度ピクセル寸法を取得するためにhttps://pixplicity.com/dp-px-converter/:あなたはこのようなこの1のように計算機を使用することができ

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

+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

関連する問題