2016-05-30 8 views
1

テストするには、以下のイメージをダウンロードして、私のドロウアブルフォルダを置いて、グライドを使用するとき、私の活動の中心にImageViewと表示されます。Androidグライドを使って相対レイアウトの背景を設定する方法

enter image description here

しかし、私はどちらか、setBackgroundしようとすると、それは私がMaukerの答えのための私のコードを編集した静止画像になったり、エラーに

setContentView(R.layout.activity_main); 
    ImageView ivImg = new ImageView(this);//to hold 

     RelativeLayout r = (RelativeLayout) findViewById(R.id.middleRlayout);//the layout of activityt 

      ivImg = (ImageView) findViewById(R.id.middle);//middle is id of img 


      Glide.with(this) 
        .load(R.drawable.giphy) 
        .asGif() 
        .crossFade() 
        .placeholder(R.drawable.giphy); 

      if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
       //noinspection deprecation 
       r.setBackgroundDrawable(R.drawable.giphy); 
      } else { 
       r.setBackground(ivImg); 
      } 

を与える:

 RelativeLayout r = (RelativeLayout) findViewById(R.id.middler); 
     ImageView ivImg = new ImageView(this); 
     ivImg = (ImageView) findViewById(R.id.middle); 
     GlideDrawableImageViewTarget imageViewTarget = new  GlideDrawableImageViewTarget(ivImg); 
     Glide.with(this) 
       .load(R.drawable.giphy) 
       .crossFade() 
       .placeholder(R.drawable.giphy) 
       .into(imageViewTarget); 

     if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
      //noinspection deprecation 
      r.setBackgroundDrawable(R.drawable.giphy); 
     } else { 
      r.setBackground(imageViewTarget); 
     } 

私の負荷とプレースホルダと同じ? setbackgroundはパラメータとして受け入れません。

答えて

1

グライドのGlideDrawableImageViewTargetを使用してください。

それはこのような何かを見てみましょう:

ImageView ivImg = findViewById(R.id.imageView); 
GlideDrawableImageViewTarget ivTarget = new GlideDrawableImageViewTarget(ivImg); 
Glide.with(this) 
    .load(R.drawable.giphy) // The image you want to load 
    .crossFade() 
    .placeholder(R.raw.sample_gif) // The placeholder GIF. 
    .into(ivTarget); 

EDITは: [OK]を、私はあなたの質問を読み違えると、あなたがRelativeLayout背景を設定することを見てきました。私の最初の考えは... Drawableが "静止画"を表しているので、アニメーションGIFを背景として設定することはできません。その点については、this questionを参照してください。 Glideの問題追跡ツールでthis related issueをチェックして詳細を確認することもできます。


第二編集:あなたの問題を解決する1つの可能な方法は、以下のレイアウトで、上記のコードを組み合わせることである。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 


    <!-- Use this ImageView to load your background image--> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop" 
     android:src="@drawable/gray"/> 

    <!-- Place the rest of your UI elements here, on this inner RelativeLayout. Like the sample below --> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <TextView 
      android:id="@+id/tv_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="My sample text" 
      android:textColor="#ffffff" 
      android:layout_centerHorizontal="true" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Click me!" 
      android:layout_below="@+id/tv_text" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 
</RelativeLayout> 

画面は次のようになります。

enter image description here

+0

これは古い方法です。 –

+1

どういう意味ですか? – Mauker

+1

私は、あなたが望むことをやり遂げる別の方法を見つけなければならないと信じています。 'RelativeLayout'バックグラウンドの設定は、画像を表示する唯一の方法ではありません。 'ImageView'をバックグラウンドとして使用し、上記の方法でイメージをロードすることができます。 – Mauker

関連する問題