android實現點選圖片全屏展示效果
阿新 • • 發佈:2020-08-28
本文例項為大家分享了android實現點選圖片全屏展示的具體程式碼,供大家參考,具體內容如下
MainActivity:
public class MainActivity extends AppCompatActivity { private ImageView imageView; private Dialog dialog; private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); //小圖的點選事件(彈出大圖) imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.show(); } }); } private void init() { imageView = (ImageView) findViewById(R.id.image); //展示在dialog上面的大圖 dialog = new Dialog(MainActivity.this,R.style.FullActivity); WindowManager.LayoutParams attributes = getWindow().getAttributes(); attributes.width = WindowManager.LayoutParams.MATCH_PARENT; attributes.height = WindowManager.LayoutParams.MATCH_PARENT; dialog.getWindow().setAttributes(attributes); image = getImageView(); dialog.setContentView(image); //大圖的點選事件(點選讓他消失) image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); } //動態的ImageView private ImageView getImageView(){ ImageView imageView = new ImageView(this); //寬高 imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); //imageView設定圖片 @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(R.drawable.lala); Drawable drawable = BitmapDrawable.createFromStream(is,null); imageView.setImageDrawable(drawable); return imageView; } }
佈局檔案:
<LinearLayout 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"> <ImageView android:id="@+id/image" android:src="@drawable/lala" android:layout_centerInParent="true" android:layout_width="200dp" android:layout_height="200dp" /> </LinearLayout>
style:
<style name="FullActivity" parent="AppTheme"> <item name="windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style>
效果圖:
沒點選:
點選後:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。