4

とにかくそこにあるので、ビットマップを に揃えることができますImageView。画像ビューに問題があります。Javaコードを介してソースを設定しています。ビットマップのサイズが変更された後、中央に配置さImageViewれますが、ビットマップを揃えることが必要です。

imageView の宣言:

    <RelativeLayout
    android:id="@+id/collection_background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/actionbar" > 


     <ImageView  
        android:id="@+id/collection_image_background"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/stampii"  />

    // countinue ........

ビットマップの設定方法は次のとおりです。

BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inTempStorage = new byte[8*1024];

    ops = BitmapFactory.decodeStream(cis,null,o2);
    ImageView view = (ImageView) findViewById (R.id.collection_image_background);        
    view.setImageBitmap(ops);

それを行う方法はありますか?

4

5 に答える 5

26

ImageViewに追加android:scaleType="fitStart"します。私は大丈夫です。

于 2013-11-18T07:41:06.583 に答える
1

実際には、画面サイズに応じてビットマップをスケーリングし、それを のソースとして設定できますImageView。次のようなものを使用できます。

int screenWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth();
    Log.e("","screen width : "+screenWidth);
    //int screenHeight = getWindow().getWindowManager().getDefaultDisplay().getHeight();

    int width = ops.getWidth();
    Log.e("","bitmap width : "+width);
    int height = ops.getHeight();
    Log.e("","bitmap height : "+height);

    float scale = 0;
    if(width>height){
        scale = (float) width / (float) height;
    } else if(height>width){
        scale = (float) height / (float) width;
    }
    Log.e("","scale : "+scale);

    float newWidth = (float) screenWidth * scale;

    Log.d("","new height : "+newWidth);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(ops, screenWidth, (int) newWidth, true);
    Log.e("","new bitmap width : "+scaledBitmap.getWidth());
    Log.e("","new bitmap height : "+scaledBitmap.getHeight());

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(screenWidth, (int)newWidth);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    view.setLayoutParams(params);
    view.setImageBitmap(scaledBitmap);

これを試してみて、うまくいくかどうかを教えてください。

于 2012-04-19T09:36:39.420 に答える
1

Android:scaleType を使用して、ImageView の配置を変更できます。

于 2012-04-19T08:07:29.080 に答える
1

この回答setLayoutParamsに示されているように、を使用できます。

編集

あなたの問題は、ロードされたビットマップがかなり薄いかもしれないのに、ビューをその親と同じ幅に指定したことです。

代わりにこの属性を使用して、ビューを必要なだけ広くするようにしてください。

android:layout_width="wrap_content"
于 2012-04-19T08:03:49.913 に答える