2015-03-02 45 views
2

我有漣漪xml。但我不確定如何在Java中獲得相同的效果。Android如何在Java中創建連鎖效果

<ripple 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:color="@color/blue_1" 
/> 

我想定義/創建/加載漣漪,甚至在Java中設置顏色。 這意味着,在Java中,我可以加載XML紋波,然後指定一種顏色。或者我可以在java中做所有事情:Ripple = new Ripple?

我試過這個鏈接的代碼:https://github.com/romainguy/google-io-2014/blob/master/app/src/main/java/com/example/android/io2014/DetailActivity.java

有一種叫做colorRipple的方法。

private void colorRipple(int id, int bgColor, int tintColor) { 
    View buttonView = findViewById(id); 

    RippleDrawable ripple = (RippleDrawable) buttonView.getBackground(); 
    GradientDrawable rippleBackground = (GradientDrawable) ripple.getDrawable(0); 
    rippleBackground.setColor(bgColor); 

    ripple.setColor(ColorStateList.valueOf(tintColor)); 
} 

我試過上面的代碼,但它給我NPE。

+0

你想要一個'RippleDrawable' – 2015-03-02 18:49:34

回答

2

您可以創建或使用像在運行時修改RippleDrawable

ColorStateList csl = ColorStateList.valueOf(Color.BLUE); 
RippleDrawable d = new RippleDrawable(csl, null, null); 

// Change the color, if desired. 
ColorStateList otherCsl = ColorStateList.valueOf(Color.RED); 
d.setColor(otherCsl); 
https://github.com/balysv/material-ripple

從Java添加紋波

+0

我想知道有沒有什麼方法可以覆蓋當前的連鎖動畫的行爲? – shivamDev 2015-03-03 11:18:04

+0

我想知道有什麼方法可以覆蓋漣漪效應動畫的當前行爲嗎? – shivamDev 2015-03-03 11:18:04

+1

不,漣漪動畫行爲不可定製。 – alanv 2015-03-04 23:50:02

0

你不能在android下的api中使用默認的rippleview 21 你需要用觸摸動畫製作自定義視圖。或者你可以很容易使用這一個紋波增加訪問量:

MaterialRippleLayout.on(view) 
      .rippleColor(Color.BLACK) 
      .create();