Saturday, July 22, 2017
Change elevation at run time by calling setZ
Change elevation at run time by calling setZ
This example show how to change elevation of ImageView programmatically, by calling setZ().
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidzelevation.MainActivity">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_margin="20dp"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold"/>
<SeekBar
android_id="@+id/z"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_progress="20"
android_max="100"/>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="0dp"
android_layout_weight="1"
android_orientation="vertical"
android_gravity="center">
<ImageView
android_id="@+id/image"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_elevation="20dp"
android_background="@android:color/background_dark"
android_src="@mipmap/ic_launcher"/>
</LinearLayout>
</LinearLayout>
package com.blogspot.android_er.androidzelevation;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
SeekBar seekBarZ;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBarZ = (SeekBar)findViewById(R.id.z);
image = (ImageView)findViewById(R.id.image);
seekBarZ.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
image.setZ(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
more:
- elevation effect of overlapped view