Reputation: 956
I've an image (image1.png)
When I click on some button, I want this image to be displayed in the middle of the screen for a second and disappear. How can I do it?
I guess that it brings me the center coordinates of the screen.
public void onClick(View button) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( dm );
int screenMiddlePointWidth = dm.widthPixels / 2;
int screenMiddlePointHeight = dm.heightPixels / 2;
}
p.s. I don't want the image to push other views on the screen so I can't set it as invisible\gone
Upvotes: 0
Views: 98
Reputation: 722
Hi use this code in xml to place your image in center
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"
android:layout_centerInParent="true"/>
</RelativeLayout>
and then in java file use the following code
ImageView img;
img=(ImageView)findViewById(R.id.imgview);
CountDownTimer timer = new CountDownTimer(1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
img.setVisibility(View.GONE);
}
};
timer.start();
you can use the timer code in onCreate method or anywhere you want.
If you want to center your image programatically use the following code.
image.setBackgroundResource(R.drawable.image1);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.gravity = Gravity.CENTER;
img.setLayoutParams(params);
Upvotes: 1