我使用以下代码来实现具有图像的自定义网格视图。如果您选择,请点击此处获取完整代码CLICK HERE以便跟随。
现在只说重要的事情。为了使我的图像在触摸时进行动画处理,我需要此代码
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/cream_background">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item
android:drawable="@color/button_selected"
android:state_pressed="true"/>
<!-- Selected -->
<item
android:drawable="@color/button_selected"
android:state_selected="true"/>
<!-- Focus -->
<item
android:drawable="@color/button_selected"
android:state_focused="true"/>
<!-- Default -->
<item android:drawable="@color/transparent"/>
</selector>
</item>
</ripple>
custom_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:id="@+id/sceneGridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_ripple"
android:gravity="center_horizontal"/>
</LinearLayout>
activity_main.xml
<GridView
android:id="@+id/sceneGrid"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:verticalSpacing="15dp"
android:numColumns="5" />
所有的神奇和问题都在我设置背景的那一行代码中发生。虽然这确实在我的imageview上产生了涟漪动画,但它却是在imageview的背后进行的。我希望动画出现在图像的顶部。因此,我尝试了几种不同的方法,比如
将整个网格背景设置为button_ripple。
<GridView
android:id="@+id/sceneGrid"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:verticalSpacing="15dp"
android:background="@drawable/button_ripple"
android:numColumns="5" />
它做了你想要的事情,现在整个网格有一个半透明的背景,无论我按哪个图像,整个网格都会从中心动画。虽然这很酷,但这不是我想要的。
将根/父背景设置为button_ripple。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@drawable/button_ripple"
android:orientation="horizontal">
该区域现在更大,填充整个网格单元(不仅仅是图片),但它并没有将其置于最前面。
将custom_grid.xml更改为RelativeLayout,并将两个ImageView叠放在一起
custom_grid.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/gridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="@+id/gridItemOverlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="@drawable/button_ripple" />
</RelativeLayout>
CustomGridAdapter.java
....
gridItemOverLay = (ImageView) findViewById(R.id.gridItemOverlay);
gridItemOverlay.bringToFront();
这个方法可以实现目标。现在底部的 ImageView 包含了我的图片,而顶部的 ImageView 做动画,成为了一个似乎在我图片上方的涟漪动画。不过,老实说这只是一种变通方法。我觉得这并不是预期的做法。所以我想问问你们有没有更好的方法或其他方式?