0

我和讓我插件使自己顯得漂亮掙扎,但小部件還擊;(設置小部件背景漸變

我有一個小部件佈局背景設置爲產生線性漸變問題

現在,我已經找到了如何生成的顏色是漸變定製「weigth」線性漸變:

public static PaintDrawable createLinearGradient(final int left, final int top, final int right, final int bottom, 
               final int[] colors, final float[] positions) 
{ 
    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() 
    { 
     @Override 
     public Shader resize(int width, int height) 
     { 
      LinearGradient lg = new LinearGradient(left, top, right, bottom, colors, positions, Shader.TileMode.REPEAT); 
      return lg; 
     } 
    }; 

    PaintDrawable gradient = new PaintDrawable(); 
    gradient.setShape(new RectShape()); 
    gradient.setShaderFactory(shaderFactory); 

    return gradient; 
} 

這裏是設置控件背景繪製從文件夾中繪製的方式:

int backgroundId = getDrawableByName(context, "round_transparrent_background"); 
remoteViews.setInt(R.id.mainLayout, "setBackgroundResource", backgroundId); 

我需要一種方法來寫該命令如下:

Drawable background = createLinearGradient(params ...); 
remoteViews.setInt(R.id.mainLayout, "setBackgroundResource", background); 

回答

1

RemoteViews固有地限於某些功能。當你考慮到你傳遞給你的任何信息需要跨進程傳遞時,這是有道理的,並不是所有的類/數據類型都被支持。

沒有方法爲任意Drawable對象傳遞給RemoteViews - 沒有的方法支持它,並Drawable一般不是類,其數據可以跨進程被整理。它可用於繪製資源的原因是資源ID只是一個整數,Android知道如何將它們充入位圖(對於png)或其各自的Drawable實現(對於用XML聲明的任何內容)。

在我看來,只可能工作的策略是使用Canvas API來實際繪製漸變爲Bitmap,並使用ImageView在AppWidget作爲背景。然後您可以撥打remoteViews.setImageViewBitmap()來設置ImageView的內容。