Jja*_*ang 17 java android ripple android-5.0-lollipop
得到以下图片视图:
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"/>
Run Code Online (Sandbox Code Playgroud)
如果我没有为图像视图设置位图,则Ripple工作得很好.但是一旦我设置了这样的位图,涟漪效应就消失了:
ImageView iv=((ImageView)rootView.findViewById(R.id.header));
iv.setImageBitmap(myBitmap);
Run Code Online (Sandbox Code Playgroud)
这是我的ripple.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
我猜这个位图隐藏了涟漪效应,我怎样才能让它可见?已经尝试过:
有任何想法吗?我见过Lollipop Contacts也是这样做的.
Alb*_*lvo 27
我有一个图像库(用a RecyclerView
和a 构建GridLayoutManager
).使用Picasso设置图像.为了给图像添加一个波纹,我ImageView
用a 包裹了FrameLayout
.项目布局是:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:foreground="@drawable/ripple"
android:clickable="true"
android:focusable="true"
>
<ImageView
android:id="@+id/grid_item_imageView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:scaleType="centerInside"
/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
请注意android:foreground
.它不一样android:background
.我尝试过没有 android:clickable="true"
,android:focusable="true"
它也有效,但它并没有伤害.
然后添加一个ripple.xml
drawable到res/drawable
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#7FF5AC8E" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
请注意,当选择项目时,这会在图像顶部显示半透明颜色(适用于版本低于Android 5.0的设备).如果您不想要它,可以将其删除.
然后将ripple.xml
带有纹波的drawable 添加到res/drawable-v21
:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/coral">
</ripple>
Run Code Online (Sandbox Code Playgroud)
您可以使用默认的涟漪效果而不是自定义ripple.xml
,但在图像上很难看到,因为它是灰色的:
android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
你可以实现它,将drawable包装在RippleDrawable中.
ImageView iv=((ImageView)rootView.findViewById(R.id.header));
Drawable image = .... // your image.
RippleDrawable rippledImage = new
RippleDrawable(ColorStateList.valueOf(rippleColor), image, null);
iv.setImageDrawable(rippledImage)
Run Code Online (Sandbox Code Playgroud)
;
对于像我这样的懒人:
android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
并根据您的风格自定义波纹颜色。
在 values/styles.xml
<style name="colorControlHighlight_blue">
<item name="colorControlHighlight">@color/main_blue_alpha26</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后,在 onCreateView() 膨胀之前的片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getContext().getTheme().applyStyle(R.style.colorControlHighlight_blue, true); //blue ripple color
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16467 次 |
最近记录: |