2016-09-20 58 views
0

enter image description here如何實現帶圖像的水平滾動並動態改變顏色?

我想要使用水平滾動視圖來實現此設計。 我已經用輪視圖庫 https://github.com/chemalarrea/Android-wheel

的幫助下,水平滾動視圖這樣做,但我沒有得到有關如何填寫這張圖像的顏色,而不會干擾使用的setBackground方法的ImageView圖像的方形區域的任何想法。

 <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="15dp"> 

       <kankan.wheel.widget.WheelView 
        android:id="@+id/slotwheel_look_color" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
      </LinearLayout> 

適配器

 private class SlotMachineAdapter_Look extends AbstractWheelAdapter { 
    // Image size 
    final int IMAGE_WIDTH = 100; 
    final int IMAGE_HEIGHT = 100; 
    private LayoutInflater mInflater; 
    private LinearLayout mLn_parant; 
    private ImageView mImg_condition; 
    // Layout inflater 
    private Context context; 

    // Slot machine symbols 
    private final int items[] = new int[]{ 
      R.drawable.image_1, 
      R.drawable.image_2, 
      R.drawable.image_3, 
      R.drawable.image_4, 
      R.drawable.image_5, 
      R.drawable.image_6, 
      R.drawable.image_7, 
      R.drawable.image_8, 
      R.drawable.image_9 
    }; 

    // Cached images 
    private List<SoftReference<Bitmap>> images; 


    /** 
    * Constructor 
    */ 
    public SlotMachineAdapter_Look(Context context) { 
     this.context = context; 
     mInflater = LayoutInflater.from(context); 
     images = new ArrayList<SoftReference<Bitmap>>(items.length); 
     for (int id : items) { 
      images.add(new SoftReference<Bitmap>(loadImage(id))); 
     } 
    } 

    /** 
    * Loads image from resources 
    */ 
    private Bitmap loadImage(int id) { 
     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id); 
     Bitmap scaled = Bitmap.createScaledBitmap(bitmap, IMAGE_WIDTH, IMAGE_HEIGHT, true); 
     bitmap.recycle(); 
     return scaled; 
    } 

    @Override 
    public int getItemsCount() { 
     return items.length; 
    } 

    // Layout params for image view 
    LayoutParams params = new LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT); 

    @Override 
    public View getItem(int index, View cachedView, ViewGroup parent) { 

     ImageView img; 
     if (cachedView != null) { 
      img = (ImageView) cachedView; 
     } else { 
      img = new ImageView(context); 
     } 
     img.setLayoutParams(params); 
     SoftReference<Bitmap> bitmapRef = images.get(index); 
     Bitmap bitmap = bitmapRef.get(); 
     if (bitmap == null) { 
      bitmap = loadImage(items[index]); 
      images.set(index, new SoftReference<Bitmap>(bitmap)); 
     } 
     BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap); 
     img.setBackground(ob); 
     img.setImageBitmap(bitmap); 

     img.setColorFilter(Color.YELLOW); 


     return img; 

//返回圖; }

} 

請幫我解決這個問題。

在此先感謝

+0

你嘗試使用'setColorFilter'或'setTint'爲你的形象?這不會干擾圖像的背景顏色。 – Mithun

+0

是的setColorFilter正在工作,但它沒有給出像這個屏幕截圖的效果。setTint不能直接使用。 – KDOSHI

+0

通過「效果像截圖」你的意思是黑暗的地區和更輕的區域? – Mithun

回答

0

使用的setColorFilter

Drawable myIcon = getResources().getDrawable(R.drawable.myicon); 
ColorFilter filter = new LightingColorFilter(Color.BLUE, Color.BLUE); 
myIcon.setColorFilter(filter); 
Imageview.setImageDrawable(myIcon); 

或者

Resources res = context.getResources(); 
final ImageView image = (ImageView) findViewById(R.id.image); 
final int newColor = res.getColor(R.color.new_color); 
image.setColorFilter(newColor, Mode.SRC_ATOP); 
+0

不提供與屏幕截圖相同的輸出。 – KDOSHI