如何为前棒棒糖创造涟漪效应

Amr*_*tha 16 android rippledrawable

如何申请像连锁反应

我已将依赖项放在app/build.gradle中

应用程序/的build.gradle

dependencies {
    compile 'com.github.traex.rippleeffect:library:1.3'
}
Run Code Online (Sandbox Code Playgroud)

的build.gradle

allprojects{
    repositories{
        jcenter()
        maven(url "https://jitpack.io" }
Run Code Online (Sandbox Code Playgroud)

XML文件:

<com.andexert.library.RippleView
        android:id="@+id/rect1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
  <Button
      android:id="@+id/enterButton"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Save your user name" />
 </com.andexert.library.RippleView>
Run Code Online (Sandbox Code Playgroud)

Java类文件

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.save_user);
    editText=(EditText) findViewById(R.id.userNameEditText);
    button=(Button) findViewById(R.id.enterButton);

    sharedPreferences=getSharedPreferences(SHARED_NAME_STRING, MODE_PRIVATE);
    String userNameString=sharedPreferences.getString(USER_NAME_STRING, "");

    editText.setText(userNameString);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String string=editText.getText().toString();
            Intent intent=new Intent(SaveUser.this, MainActivity.class);
            intent.putExtra("user", string);

            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString(USER_NAME_STRING, string);
            editor.commit();

            startActivity(intent);

        }
    });
}
Run Code Online (Sandbox Code Playgroud)

它工作但我的问题是在涟漪效果完成之前打开另一个活动,当我按下后退按钮时剩余的涟漪完成.我该怎么解决?

小智 32

对于棒棒糖(API> 21),在drawable-v21中将文件设为btn_ripple_effect.xml并放在代码下面

 <?xml version="1.0" encoding="utf-8"?>

    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="?android:colorAccent"
        tools:targetApi="lollipop">
        <item android:drawable="@color/cancel_btn_clr" /> <!-- default -->
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="?android:colorAccent" />
            </shape>
        </item>
    </ripple>
Run Code Online (Sandbox Code Playgroud)

对于pre lollipop(API <21),将文件作为btn_ripple_effect.xml放在drawable文件夹中并放在代码下面

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/colorAccent"></solid>
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@color/cancel_btn_clr"></solid>
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

创建按钮如下

<Button
                    android:id="@+id/button3"
                    style="@style/cancel_btn_style"
                    android:layout_marginLeft="50dp"
                    android:text="Cancel"
                    />
Run Code Online (Sandbox Code Playgroud)

在style.xml中添加它

          <style name="cancel_btn_style" parent="Theme.AppCompat">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#ffffff</item>
<item name="android:layout_height">36dp</item>
<item name="android:layout_width">90dp</item>
<item name="android:background">@drawable/btn_ripple_effect</item>
Run Code Online (Sandbox Code Playgroud)

  • 对于pre-lollipop设备,btn_ripple_effect.xml中的父标签是什么? (2认同)

Far*_*uzi 25

你可以尝试这个库balysv/material-ripple.

在您的gradle中,添加以下行:

compile 'com.balysv:material-ripple:1.0.2'
Run Code Online (Sandbox Code Playgroud)

这是怎么做的:

<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Button inside a ripple"/>
</com.balysv.materialripple.MaterialRippleLayout>
Run Code Online (Sandbox Code Playgroud)


小智 5

对于lollipop(API> 21),在drawable-v21中将文件制作为btn_ripple_effect.xml,并将其放在下面的代码中

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="#f8c7c9c8"
    tools:targetApi="lollipop">

    <item android:id="@android:id/mask">

        <shape android:shape="oval">
            <corners android:radius="@dimen/dp10" />
            <solid android:color="#f8ced6d2" />

        </shape>
    </item>
</ripple>
Run Code Online (Sandbox Code Playgroud)

对于棒棒糖(API <21)之前的版本,将文件作为btn_ripple_effect.xml放在drawable文件夹中,并放在下面的代码中

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

    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#f8ced6d2"/>
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@color/transparent"/>
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

像这样在imageview上使用它

<ImageView
  android:id="@+id/imageViewOffer"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerVertical="true"
  android:background="@drawable/btn_ripple_effect"
  android:src="@mipmap/ic_offers"/>
Run Code Online (Sandbox Code Playgroud)