2015-11-28 16 views

答えて

26

このようにRelativeLayoutに画像を読み込むことができます。私はちょうどあなたに背景にイメージを設定している難しい部分を示しています。キャッシングに

4.X前グライドバージョンについては

Glide.with(this).load(imageViewPath).asBitmap().into(new SimpleTarget<Bitmap>(relLayoutWidth, relLayoutHeight) { 
    @Override 
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 
     Drawable drawable = new BitmapDrawable(context.getResources(), resource); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      yourRelativeLayout.setBackground(drawable); 
     } 
    } 
}); 

、このページを参照してください:Caching and Cache Invalidation。ジッドのため

アップデート以降V4:

GlideApp.with(this).load(R.drawable.backgroundimage).into(new SimpleTarget<Drawable>() { 
      @Override 
      public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
        yourRelativeLayout.setBackground(resource); 
       } 
      } 
     }); 
+0

なぜ 'Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN'が必要ですか? – jaynp

+0

@ dal102これは私のアプリケーションに特化したものです –

+1

これがなければ 'APIレベル16(現在の最小値はXXです)を呼び出すにはアンドロイド。view.View#setBackground' - >という警告メッセージが表示されますので、この解決法はJELLY_BEANとより高いAPI – thorin86

6

を、私は、このクラスは、さまざまなシナリオで自動的にグライドによって処理される特定のメソッドを持っているので、それを達成するための最良の方法は、あなた自身のViewTarget実装と作品だと思います。

ViewGroup(LinearLayout、RelativeLayoutなど)の抽象実装です。

public abstract class ViewGroupTarget<Z> extends ViewTarget<ViewGroup, Z> implements GlideAnimation.ViewAdapter { 

    public ViewGroupTarget(ViewGroup view) { 
     super(view); 
    } 

    /** 
    * Returns the current {@link android.graphics.drawable.Drawable} being displayed in the view using 
    * {@link android.widget.ImageView#getDrawable()}. 
    */ 
    @Override 
    public Drawable getCurrentDrawable() { 
     return view.getBackground(); 
    } 

    /** 
    * Sets the given {@link android.graphics.drawable.Drawable} on the view using 
    * {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}. 
    * 
    * @param drawable {@inheritDoc} 
    */ 
    @Override 
    public void setDrawable(Drawable drawable) { 
     view.setBackground(drawable); 
    } 

    /** 
    * Sets the given {@link android.graphics.drawable.Drawable} on the view using 
    * {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}. 
    * 
    * @param placeholder {@inheritDoc} 
    */ 
    @Override 
    public void onLoadStarted(Drawable placeholder) { 
     view.setBackground(placeholder); 
    } 

    /** 
    * Sets the given {@link android.graphics.drawable.Drawable} on the view using 
    * {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}. 
    * 
    * @param errorDrawable {@inheritDoc} 
    */ 
    @Override 
    public void onLoadFailed(Exception e, Drawable errorDrawable) { 
     view.setBackground(errorDrawable); 
    } 

    /** 
    * Sets the given {@link android.graphics.drawable.Drawable} on the view using 
    * {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}. 
    * 
    * @param placeholder {@inheritDoc} 
    */ 
    @Override 
    public void onLoadCleared(Drawable placeholder) { 
     view.setBackground(placeholder); 
    } 

    @Override 
    public void onResourceReady(Z resource, GlideAnimation<? super Z> glideAnimation) { 

     this.setResource(resource); 
    } 

    protected abstract void setResource(Z resource); 

} 

具体的な実装(この場合はLinearLayout)。

public class LinearLayoutTarget extends ViewGroupTarget<Bitmap> { 

    private Context context; 

    public LinearLayoutTarget(Context context, LinearLayout linearLayout) { 

     super(linearLayout); 

     this.context = context; 
    } 

    /** 
    * Sets the {@link android.graphics.Bitmap} on the view using 
    * {@link android.widget.ImageView#setImageBitmap(android.graphics.Bitmap)}. 
    * 
    * @param resource The bitmap to display. 
    */ 
    @Override 
    protected void setResource(Bitmap resource) { 

     view.setBackground(new BitmapDrawable(context.getResources(), resource)); 
    } 

} 

Glide.with(this.getApplicationContext()) 
       .load(R.drawable.your_image) 
       .asBitmap() 
       .into(new LinearLayoutTarget(this.getApplicationContext(), (LinearLayout) yourLinearLayoutInstanceHere)); 

ビットマップを使用しない場合でも、さらに簡単な作業です。

具体的な実装。

public class LinearLayoutTarget extends ViewGroupTarget<Drawable> { 

    public LinearLayoutTarget(LinearLayout linearLayout) { 

     super(linearLayout); 
    } 

    /** 
    * Sets the {@link android.graphics.Bitmap} on the view using 
    * {@link android.widget.ImageView#setImageBitmap(android.graphics.Bitmap)}. 
    * 
    * @param resource The bitmap to display. 
    */ 
    @Override 
    protected void setResource(Drawable resource) { 

     view.setBackground(resource); 
    } 

} 

Glide.with(this.getApplicationContext()) 
       .load(R.drawable.your_image) 
       .into(new LinearLayoutTarget((LinearLayout) yourLinearLayoutInstanceHere)); 
+2

実際には 'ViewGroupTarget 'ではなく 'Drawable'でなければなりません。 – AndyRoid

関連する問題