android实现点击图片转到一个新的全屏显示,并可以放大和缩小,请给出代码
时间: 2024-05-15 12:19:44 浏览: 194
以下是一个简单的实现点击图片进入全屏显示并可以放大缩小的示例代码:
MainActivity.java
```
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FullScreenImageDialogFragment.newInstance(imageView.getDrawable()).show(getSupportFragmentManager(), "dialog");
}
});
}
}
```
activity_main.xml
```
<?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/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image"/>
</RelativeLayout>
```
FullScreenImageDialogFragment.java
```
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class FullScreenImageDialogFragment extends DialogFragment implements View.OnTouchListener {
private Drawable drawable;
private ImageView imageView;
public static FullScreenImageDialogFragment newInstance(Drawable drawable) {
FullScreenImageDialogFragment fragment = new FullScreenImageDialogFragment();
Bundle args = new Bundle();
args.putParcelable("drawable", drawable);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawable = getArguments().getParcelable("drawable");
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_full_screen_image_dialog, container, false);
imageView = rootView.findViewById(R.id.dialog_image_view);
imageView.setImageDrawable(drawable);
imageView.setOnTouchListener(this);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//设置全屏显示
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
//处理缩放和拖动
ImageView view = (ImageView) v;
view.setScaleType(ImageView.ScaleType.MATRIX);
float scale;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: //手指按下
break;
case MotionEvent.ACTION_MOVE: //手指移动
if (event.getPointerCount() == 1) { //单指拖动
view.setTranslationX(event.getX() - view.getWidth() / 2);
view.setTranslationY(event.getY() - view.getHeight() / 2);
} else if (event.getPointerCount() == 2) { //双指缩放
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
scale = (float) Math.sqrt(x * x + y * y);
if (scale > 10) {
float scaleFactor = scale / 200;
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
}
}
break;
case MotionEvent.ACTION_UP: //手指抬起
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
```
fragment_full_screen_image_dialog.xml
```
<?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/dialog_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</RelativeLayout>
```
注意:上述示例中的代码仅为演示用途,可能存在一些局限性和不足之处。实际开发中需要根据具体需求进行调整和改进。
阅读全文