Android 如何在imageView点击时全屏显示imageView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24463691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show imageView full screen on imageView click?
提问by Amit Jayaswal
I am getting images from url and showing it on the imageView. This functionality is working properly. But I want that when I click on that image, then it must be full screen. So how to achieve this functionality? I know I am missing something. Please help me. Screenshot is attached. I want the same functionality in my app also.
我正在从 url 获取图像并将其显示在 imageView 上。此功能正常工作。但是我希望当我单击该图像时,它必须是全屏的。那么如何实现这个功能呢?我知道我错过了一些东西。请帮我。附上截图。我也希望在我的应用程序中具有相同的功能。
Here is my code, which I am trying on Image click:
这是我的代码,我正在尝试单击图像:
@Override
public void onClick(View v) {
if (isTouch1) {
horizontalScrollView.setVisibility(View.GONE);
isTouch1 = false;
// mSystemUiHider.toggle();
setTheme(R.style.FullscreenTheme);
Log.d("Here isTouch is true", ">");
// ChangeThemeUtils.changeToTheme(FullScreenImageAdapter.this, ChangeThemeUtils.THEME_HIDE_ALL_WINDOW);
getSupportActionBar().hide();
} else {
isTouch1 = true;
horizontalScrollView.setVisibility(View.VISIBLE);
getSupportActionBar().show();
setTheme(R.style.ExampleTheme);
//mSystemUiHider.show();
Log.d("Here isTouch is false", ">");
}
}
采纳答案by Amit Jayaswal
Yes I got the trick.
是的,我明白了。
public void onClick(View v) {
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ){
imgDisplay.setSystemUiVisibility( View.SYSTEM_UI_FLAG_HIDE_NAVIGATION );
}
else if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
imgDisplay.setSystemUiVisibility( View.STATUS_BAR_HIDDEN );
else{}
}
But it didn't solve my problem completely. I want to hide the horizontal scrollview too, which is in front of the imageView (below), which can't be hidden in this.
但它并没有完全解决我的问题。我也想隐藏水平滚动视图,它在 imageView 前面(下面),不能隐藏在这个里面。
回答by Haresh Chhelana
You can use ImageViewbelow two properties to show image based on your requirement :
您可以使用ImageView下面的两个属性来根据您的要求显示图像:
android:adjustViewBounds: Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
android:scaleType:Controls how the image should be resized or moved to match the size of this ImageView
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@drawable/ic_launcher"/>
android:adjustViewBounds:如果您希望 ImageView 调整其边界以保留其可绘制对象的纵横比,请将其设置为 true。
android:scaleType:控制如何调整或移动图像以匹配此 ImageView 的大小
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@drawable/ic_launcher"/>
Above two properties can be use either xml or java code.
以上两个属性可以使用 xml 或 java 代码。
As you need to decide at run time need to show image into full screen or not so will apply above two properties at java code as below :
由于您需要在运行时决定是否需要全屏显示图像,因此将在 Java 代码中应用以上两个属性,如下所示:
public class MainActivity extends Activity {
ImageView imageView;
boolean isImageFitToScreen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isImageFitToScreen) {
isImageFitToScreen=false;
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
imageView.setAdjustViewBounds(true);
}else{
isImageFitToScreen=true;
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
});
}
}
回答by Mansukh Ahir
Use Immersive Full-Screen Mode
使用沉浸式全屏模式
call fullScreen()
on ImageView
click.
调用fullScreen()
上ImageView
点击。
public void fullScreen() {
// BEGIN_INCLUDE (get_current_ui_flags)
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
int newUiOptions = uiOptions;
// END_INCLUDE (get_current_ui_flags)
// BEGIN_INCLUDE (toggle_ui_flags)
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.i(TAG, "Turning immersive mode mode off. ");
} else {
Log.i(TAG, "Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
//END_INCLUDE (set_ui_flags)
}
Read more
阅读更多
Example Download
示例下载
回答by MFP
use following property of ImageView for full size of image
使用 ImageView 的以下属性获取完整大小的图像
android:scaleType="fitXY"
ex :
前任 :
<ImageView
android:id="@+id/tVHeader2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:gravity="center"
android:src="@drawable/done_sunheading" />
1)Switch on other activity when click on image.
1)点击图片打开其他活动。
2)pass url in intent
2)在意图中传递网址
3)Take imageview on that Activity and set above property of imageview
3)对该活动进行图像视图并设置图像视图的上述属性
4)Get the url from intent and set that image.
4)从意图获取网址并设置该图像。
but using this your image may be starched if it will of small size.
但是使用它,如果尺寸较小,您的图像可能会变硬。
回答by ainos
That didn't work for me, I used some code parts from web, what I did:
这对我不起作用,我使用了来自网络的一些代码部分,我做了什么:
new activity: FullScreenImagewith:
新活动:全屏图像:
package yourpackagename;
import yourpackagename.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class FullScreenImage extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_full);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
ImageView imgDisplay;
Button btnClose;
imgDisplay = (ImageView) findViewById(R.id.imgDisplay);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FullScreenImage.this.finish();
}
});
imgDisplay.setImageBitmap(bmp );
}
}
Then create a xml: layout_full
然后创建一个xml:layout_full
<?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" >
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textColor="#ffffff"
android:text="Close" />
</RelativeLayout>
And finally, send the image name from your mainactivity
最后,从您的主要活动发送图像名称
final ImageView im = (ImageView)findViewById(R.id.imageView1) ;
im.setBackgroundDrawable(getResources().getDrawable(id1));
im.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NAMEOFYOURCURRENTACTIVITY.this, FullScreenImage.class);
im.buildDrawingCache();
Bitmap image= im.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
}
});
回答by Achiever
Use this property for an Image view such as,
将此属性用于图像视图,例如,
1) android:scaleType="fitXY"
- It means the Images will be stretched to fit all the sides of the parent that is based on your ImageView
!
1) android:scaleType="fitXY"
- 这意味着图像将被拉伸以适应基于您的ImageView
!
2) By using above property, it will affect your Image resolution so if you want to maintain the resolution then add a property such as android:scaleType="centerInside"
.
2)通过使用上述属性,它会影响您的图像分辨率,因此如果您想保持分辨率,请添加一个属性,例如android:scaleType="centerInside"
.
回答by Rajaji subramanian
public class MainActivity extends Activity {
ImageView imgV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgV= (ImageView) findViewById("your Image View Id");
imgV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgV.setScaleType(ScaleType.FIT_XY);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();
}
}
});
}
}
回答by Raktim Bhattacharya
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="fitXY"
android:layout_marginTop="5dp"
android:layout_marginBottom="60dp"
android:src="@drawable/lwt_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:visibility="visible"
android:id="@+id/imageView"
android:layout_centerInParent="true"/></RelativeLayout>
and in activity
并在活动中
final ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
PhotoViewAttacher photoAttacher;
photoAttacher= new PhotoViewAttacher(imageView);
photoAttacher.update();
Picasso.with(ImageGallery.this).load(imageUrl).placeholder(R.drawable.lwt_placeholder)
.into(imageView);
Thats it!
就是这样!
回答by Jhoan River
Actually there are three ways to enable full screnn, visit : https://developer.android.com/training/system-ui/immersive
实际上有三种方法可以启用全屏,请访问:https: //developer.android.com/training/system-ui/immersive
but if you wanna get full screen when the activity is opened, just put this code in your_activity.java
但是如果你想在活动打开时全屏显示,只需将此代码放在 your_activity.java 中
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
private void hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
回答by C.Scott
It's easy to achieve this is to just use an Intent like this: (I put the method in a custom class that takes in an Activity as a parameter so it can be called from any Fragment or Activity)
很容易实现这一点,只需使用这样的 Intent :(我将该方法放在一个自定义类中,该类将 Activity 作为参数,以便可以从任何 Fragment 或 Activity 调用它)
public class UIutils {
private Activity mActivity;
public UIutils(Activity activity){
mActivity = activity;
}
public void showPhoto(Uri photoUri){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(photoUri, "image/*");
mActivity.startActivity(intent);
}
}
Then to use it just do this:
然后使用它只需执行以下操作:
imageView.setOnClickListener(v1 -> new UIutils(getActivity()).showPhoto(Uri.parse(imageURI)));
I use this with an Image URL but it can be used with stored files as well. If you are accessing images form the phones memory you should use a content provider.
我将它与图像 URL 一起使用,但它也可以与存储的文件一起使用。如果您从手机内存访问图像,您应该使用内容提供商。