这是在棒棒糖前设备上产生涟漪效应的正确方法吗? [英] Is this the right way to get the ripple effect on pre-lollipop devices?

查看:88
本文介绍了这是在棒棒糖前设备上产生涟漪效应的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

材料设计"已经问世,它具有一些新的魔术功能,这些功能很难使用普通技能进行编码,这些功能据说只能在硅谷找到.双关打算.

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. Pun intended.

无论如何,我试图使Ripple效果在API< 21,即棒棒糖之前的设备上工作.从最直接的意义上来说,涟漪效应就是水.

Anyway, I'm trying to get the Ripple effect working on API<21, that is, pre-lollipop devices. 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.

这是我的活动.java:

Here's my 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);
    }
}

这是布局:

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

请转到您使用的IDE,看看有没有第二个可运行对象,发生了什么.

Please head over to the IDE you use and see what happens, with or without the second runnable.

以下是屏幕截图:

一个人可以单独运行:一个

Runnable one alone :One

两个人单独运行:两个

两者:与之相同,尽管两者相同.

一个

推荐答案

对于棒棒糖(API> 21),请使用此

For lollipop(API>21) use this

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

棒棒糖前(API< 21)

For pre lollipop (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>

这篇关于这是在棒棒糖前设备上产生涟漪效应的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆