单击所有应用程序的效果/动画

时间:2017-01-06 16:12:36

标签: java android animation click

可以为我的应用程序上的所有点击创建点击效果/动画吗?

example

由于我希望我的应用程序上的所有点击事件都有这种行为,我应该怎么做?这对智能手机的性能或资源有害吗?

2 个答案:

答案 0 :(得分:0)

如果不了解已经构建的堆栈,很难知道,据说我觉得有一些更安全且更少的方法可以解决所有相同的onclick事件。对于一个我不会改变“onClick”功能的基本性质,更低级别你更混乱它更危险。话虽如此,我想我会创建自己的onclick版本/功能,也许是boomClick,其中boomClick创建你想要的动画。引用单个函数几乎不会降低性能。

答案 1 :(得分:0)

所以,经过一天工作后,我设法完成了预期的行为。

基本上,我创建了自己的Activity类,它将在自定义lib的帮助下完成动画工作。我将尝试解释我的所作所为以供参考:

<强> 1。将此lib添加到您的项目中:

compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.3'

<强> 2。将这些尺寸添加到&#34; dimens.xml&#34;文件:

<dimen name="click_animation">100dp</dimen>
<dimen name="click_compensation">50dp</dimen>

第3。将活动布局的顶级父级设为&#34; RelativeLayout&#34;并设置自定义ID:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">

    ... the rest of the layout ...

</RelativeLayout>

<强> 4。创建自己的&#34;活动&#34;类:

public class MyActivity extends AppCompatActivity {

    private RelativeLayout.LayoutParams params;

    public void setClickAnimation(final Activity activity) {
        // if you want to change the size of the animation, change the size on the dimens.xml
        int size = (int) activity.getResources().getDimension(R.dimen.click_animation);
        params = new RelativeLayout.LayoutParams(size, size);

        // you want the parent layout of the activity
        final RelativeLayout view = (RelativeLayout) activity.findViewById(R.id.main_layout);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // you maybe won't need this compensation value
                    int compensation = (int) activity.getResources().getDimension(R.dimen.click_compensation);
                    try { startAnimation(view, (int) event.getX() - compensation, (int) event.getY() - compensation); }
                    catch (IOException e) { e.printStackTrace(); }
                }
                return true;
            }
        });
    }

    private void startAnimation(RelativeLayout view, int x, int y) throws IOException {
        params.leftMargin = x;
        params.topMargin = y;

        // those are from the lib you imported
        final GifImageView anim = new GifImageView(this);
        // if you don't have it yet, put the gif you want on the assets folder
        final GifDrawable gifFromResource = new GifDrawable(getAssets(), "click_animation.gif");
        gifFromResource.addAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationCompleted(int loopNumber) {
                anim.setVisibility(View.GONE);
                gifFromResource.stop();
                gifFromResource.recycle();
            }
        });

        anim.setBackground(gifFromResource);
        gifFromResource.start();

        view.addView(anim, params);
    }
}

<强> 5。让您的活动扩展您的活动&#34;类:

public class FirstScreen extends MyActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cards_screen);

        // call the method you created and pass the activity context
        setClickAnimation(this);
    }
}

至于花费的资源:这看起来是一个很好的解决方案,我的表现很好。该应用程序似乎并没有浪费大量资源。