简体   繁体   English

这是在棒棒糖前设备上产生涟漪效应的正确方法吗?

[英]Is this the right way to get the ripple effect on pre-lollipop devices?

Material Design is out, and with it are some of the new magic features that have been hard to code using ordinary skills, which are supposedly only found in the Silicon Valley. Material Design已经淘汰,其中包含一些新的魔术功能,这些功能很难使用普通技能进行编码,这些功能据说只能在硅谷找到。 Pun intended. 双关打算。

Anyway, I'm trying to get the Ripple effect working on API<21, that is, pre-lollipop devices. 无论如何,我试图使Ripple效果在API <21(即棒棒糖之前的设备)上运行。 In its most literal sense, the ripple effect, water, for that matter. 从最直接的意义上讲,涟漪效应就是水。

Here's what I have tried. 这是我尝试过的。 The approach is simple. 该方法很简单。 I colour three layouts gray, three white, then the other three white and the three gray and then shut up. 我将三个布局的颜色设置为灰色,三个白色,然后其他三个白色,三个灰色然后关闭。

However, the implementation has not worked. 但是,该实现未起作用。 Only the second runnable runs and the effect is not materialised. 仅第二次可运行,并且未实现效果。

Here's my activity.java : 这是我的activity.java:

package dreamnyc.ripple;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity {
    public String TAG = "okay";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button b;
        final RelativeLayout la1;
        final LinearLayout la2, la3, la4, la5, la6, la7;

        la1 = (RelativeLayout) findViewById(R.id.l1);
        la2 = (LinearLayout) findViewById(R.id.l2);
        la3 = (LinearLayout) findViewById(R.id.l3);
        la4 = (LinearLayout) findViewById(R.id.l4);
        la5 = (LinearLayout) findViewById(R.id.l5);
        la6 = (LinearLayout) findViewById(R.id.l6);
        la7 = (LinearLayout) findViewById(R.id.l7);

        b = (Button) findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                la7.setBackgroundColor(Color.LTGRAY);


                final Runnable r1 = new Runnable() {
                    @Override
                    public void run() {
                        synchronized (this) {
                            try {

                                la7.setBackgroundColor(Color.WHITE);
                                la6.setBackgroundColor(Color.LTGRAY);
                                la4.setBackgroundColor(Color.LTGRAY);
                                la2.setBackgroundColor(Color.LTGRAY);
                                la5.setBackgroundColor(Color.WHITE);
                                la3.setBackgroundColor(Color.WHITE);
                                la1.setBackgroundColor(Color.WHITE);
                                wait(100);


                            } catch (InterruptedException e) {

                                Log.d(TAG, "exception encountered" + e);

                            }

                            synchronized (this) {
                                try {


                                    final Runnable r2 = new Runnable() {
                                        @Override
                                        public void run() {
                                            la7.setBackgroundColor(Color.WHITE);
                                            la5.setBackgroundColor(Color.LTGRAY);
                                            la3.setBackgroundColor(Color.LTGRAY);
                                            la1.setBackgroundColor(Color.LTGRAY);
                                            la6.setBackgroundColor(Color.WHITE);
                                            la4.setBackgroundColor(Color.WHITE);
                                            la2.setBackgroundColor(Color.WHITE);

                                        }


                                    };
                                    wait(10);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                            }



                        }

                    }
                };

                runOnUiThread(r1);

            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here's the layout : 这是布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/l1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:padding="15dp"
        android:id="@+id/l2">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:padding="15dp"
            android:id="@+id/l3">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:padding="15dp"
                android:id="@+id/l4">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:padding="15dp"
                    android:id="@+id/l5">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center_horizontal"
                        android:padding="15dp"
                        android:id="@+id/l6">

                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:padding="15dp"
                            android:id="@+id/l7">


                            <Button
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:text="Ripple"
                                android:id="@+id/button"
                                android:layout_marginTop="130dp" />
                        </LinearLayout>
                    </LinearLayout>


                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Please head over to the IDE you use and see what happens, with or without the second runnable. 请转到您使用的IDE,看看有没有第二个可运行对象,发生了什么。

Here are the screenshots : 这是屏幕截图:

Runnable one alone : One 单独运行的一个: 一个

Runnable two alone : Two 可单独运行两个: 两个

Both : Same as one, though both. 两者: 尽管两者都相同。

One

For lollipop(API>21) use this 对于棒棒糖(API> 21)使用此

<?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>

For pre lollipop (API<21) 用于棒棒糖前(API <21)

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM