按下时如何全屏显示图像?

时间:2018-09-10 20:02:05

标签: java android

我正在使用SlidingSplashScreen库在图像之间滑动并在图像下方显示小点,以跟踪所显示的图像。所以我的问题是当按下时如何全屏显示图像?我已经尝试过@Haresh Chhelana的以下解决方案,但是没有用:

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);
            }
        }
    });

  }
}

以下是我的代码,主要活动是:

package com.hodhod.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;



import com.chabbal.slidingdotsplash.SlidingSplashView;

public class MainActivity extends AppCompatActivity {

private int[] images = {

        R.drawable.escapers,
        R.drawable.escapers2,
        R.drawable.facebook,
        R.drawable.facebook


};

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final SlidingSplashView slidingSplashView = (SlidingSplashView) findViewById(R.id.sliding_splash_view);

    slidingSplashView.setImageResources(images);


  }

}

那是activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">


<com.chabbal.slidingdotsplash.SlidingSplashView
    android:id="@+id/sliding_splash_view"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_alignParentTop="true">

</com.chabbal.slidingdotsplash.SlidingSplashView>



</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您必须在图像上添加clickListener。像这样:

@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
  inflater = (LayoutInflater)activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  final View itemView = inflater.inflate(R.layout.viewpager_item, container,false);

  final ImageView image = (ImageView)itemView.findViewById(R.id.imageView);
  DisplayMetrics dis = new DisplayMetrics();
  activity.getWindowManager().getDefaultDisplay().getMetrics(dis);
  int height = dis.heightPixels;
  int width = dis.widthPixels;
  image.setMinimumHeight(height);
  image.setMinimumWidth(width);

  itemView.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        // here you can show the image in fullscreen
        // First method : use an hidden fullscreen Layout (like the first piece of code you've posted)
        // Second method : use an Activity
    }
  try {
    Picasso.with(activity.getApplicationContext())
            .load(images[position])
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(image);
  }
  catch (Exception ex){

  }

  container.addView(itemView);
  return itemView;
}