質問

私は実装することを要求するプロジェクトに取り組んでいます 2つの水平スクロールビューその間の画像ビュー 彼ら。画像ビューのサイズを拡張して、スクロールビュー間のギャップを埋めたいと思います。サンプルコードを調べましたが、画像ビューのサイズのどこにも言及していないようです。私の問題を説明する画像の後、以下のコードを囲みました。

enter image description here

public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;

private Integer[] Imgid = {
    R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery1 = (Gallery)findViewById(R.id.examplegallery1);
gallery1.setAdapter(new AddImgAdp(this));

imgView = (ImageView)findViewById(R.id.ImageView01);    
imgView.setImageResource(Imgid[0]);


 gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        imgView.setImageResource(Imgid[position]); 
    }
});

}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg =  typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
}

public int getCount() {
    return Imgid.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
}
}

これは私のXMLメインファイルです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Gallery
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

以下は私の属性ファイルです。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GalleryTheme">
    <attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
役に立ちましたか?

解決

ヴィナー、

ここでの問題は、本当に2つの問題です。

  1. ImageViewのサイズは親に基づいています。つまり、幅を共有することを意味します 身長。

  2. 画像は、最も制限的な次元(幅)に一致するようにサイズ変更されています。

これを処理する最も簡単な方法は、ImageViewのScaletypeを設定することです。多くのスケーリングタイプがありますが、おそらくあなたが望むものは 中央作物. 。コードでは、これはによって行われます view_name.setScaleType(ImageView.ScaleType.CENTER_CROP). 。属性を使用してImageViewについてXMLでこれを行うこともできます android:scaleType="centerCrop". 。気をつけて、これはあなたのイメージをサイズ変更し、あなたのアスペクト比を維持しますが、それは側面を切り落とします。

上記の属性をXMLのImageViewに追加するだけで(おそらくLayout_Weightの下)、必要なことを行う必要があります。

お役に立てれば、

Fuzzicallogic

他のヒント

以下のコードを試してみませんか...?

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="0dp" 
android:src="@drawable/ic_launcher"
android:layout_weight="1"/>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>

それが機能しない場合は、ギャラリーの高さをDPに設定してみてください。

ImageViewのサイズは親に基づいています。つまり、幅と高さを共有します。また、画像は、最も制限的な次元(幅)に一致するようにサイズ変更されています。

AddimGADPクラスでは、ImageViewの高さと幅を特定のピクセルサイズに設定します。

imgView.setLayoutParams(new Gallery.LayoutParams(110, 100)); //width = "110px", height="100px"

私は個人的にレイアウトをlatativeLayoutとして配置し、最初のギャラリーを上部、2番目のギャラリーを下に並べ、ndには2番目のギャラリーの上に並べるImageViewセットがあります。次に、画像を作成します。

imgView.setLayoutParams( new Gallery.LayoutParams(
   Gallery.LayoutParams.FILL_PARENT, 
   Gallery.LayoutParams.FILL_PARENT) );

ImageViewをLinearLayoutでFill_Parentに設定すると、画面の下部に入力され、2番目のギャラリーが画面から外れます。

また、すべてのスペースを埋めることで、写真を歪める可能性が高いことを知ってください。すべてのサイズ変更オプションを検討してください。 http://developer.android.com/reference/android/widget/imageview.scaletype.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top