代码写个九宫格布局显示图片

不在xml中设置布局,在代码中直接写个布局,显示下载的图片,如下图所示,图片有点丑
这里写图片描述

XML中添加个linearLayout

<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="${relativePackage}.${activityClass}" ><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="九宫格显示图片" /><LinearLayoutandroid:id="@+id/report_photo_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dip"android:orientation="vertical" android:layout_below="@+id/text"/>
</RelativeLayout>

另外加个布局,显示图片view_files_image.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"android:gravity="center" ><ImageViewandroid:id="@+id/image"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerHorizontal="true"android:scaleType="centerCrop" />
</RelativeLayout>

其他就是代码了:

public class MainActivity extends Activity {private LinearLayout layoutPhotos;//private LinkedHashMap<String, String> choosePhotos = new LinkedHashMap<String, String>();//已选择的照片private DisplayMetrics dm = new DisplayMetrics();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);layoutPhotos = (LinearLayout) findViewById(R.id.report_photo_layout);//choosePhotos哦让其中添加图片地址choosePhotos.put("1", "http://lllqmw.qiniudn.com/1.png");choosePhotos.put("2", "http://lllqmw.qiniudn.com/2.png");choosePhotos.put("3", "http://lllqmw.qiniudn.com/3.png");choosePhotos.put("4", "http://lllqmw.qiniudn.com/4.png");choosePhotos.put("5", "http://lllqmw.qiniudn.com/5.png");choosePhotos.put("6", "http://lllqmw.qiniudn.com/6.png");choosePhotos.put("7", "http://lllqmw.qiniudn.com/7.png");choosePhotos.put("8", "http://lllqmw.qiniudn.com/8.png");//显示图片layoutShowFiles();}/*** 九宫格的方式显示下载的图片*/private void layoutShowFiles() {layoutPhotos.removeAllViews();// 每行四张视图,计算一下第个的宽高getWindowManager().getDefaultDisplay().getMetrics(dm);int margins = (int) dm.density * 2;int photoWidth = (int) (((dm.widthPixels - dm.density * 30) / 3) - margins * 2.5);Log.i("info", "每个view的尺寸为:" + photoWidth + ",间距为:" + margins);LinearLayout subLayout = new LinearLayout(this);subLayout.setOrientation(LinearLayout.HORIZONTAL);LayoutParams subParams = new LayoutParams(photoWidth, photoWidth);subParams.setMargins(margins, margins, margins, margins);int i = 0;Iterator<Entry<String, String>> iter = choosePhotos.entrySet().iterator();while (iter.hasNext()) {subLayout.addView(createLayoutPhoto(i, iter.next().getValue(), subParams));if (i % 3 == 2) {layoutPhotos.addView(subLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));subLayout = new LinearLayout(this);subLayout.setOrientation(LinearLayout.HORIZONTAL);}i++;}// 计算一下sublayout还能放几个view,如果已经摆満了4个,刚新起一行if ((3 - choosePhotos.size() % 3) == 3) {subLayout = new LinearLayout(this);subLayout.setOrientation(LinearLayout.HORIZONTAL);}layoutPhotos.addView(subLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));}/*** 生成一个图片* @param id* @param path* @param params* @return*/private View createLayoutPhoto(final int imagePosition, final String path, LayoutParams params) {View v = getLayoutInflater().inflate(R.layout.view_files_image, null);v.setLayoutParams(params);ImageLoader.getInstance().displayImage(path, (ImageView) v.findViewById(R.id.image),SoftApplication.imageOptions);return v;}}

下载图片需要的application

public class SoftApplication extends Application {	public static DisplayImageOptions imageOptions ;@Overridepublic void onCreate() {super.onCreate();initImageLoader(getApplicationContext());//设置图片属性:下载时的图片,下载失败的图片,是否圆角imageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).showImageOnLoading(R.drawable.default_loading)     // 加载开始默认的图片.showImageForEmptyUri(R.drawable.default_loading)   // url爲空會显示该图片,自己放在drawable里面的.showImageOnFail(R.drawable.default_loading)        // 加载图片出现问题,会显示该图片.imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();}public static void initImageLoader(Context context) {ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove for release app.build();ImageLoader.getInstance().init(config);}	}

有个地方容易忘记:AndroidManifest.xml 声明

  <applicationandroid:name="com.example.demo_imagedisplay.SoftApplication"

ok了,点击Demo_imageDisplay下载完整代码。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/250713.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

Android图片九宫格

九宫格分三类 实现的效果 具体实现 activity_main

用Python处理图片九宫格

本篇阅读时间约为 5 分钟。文/咪哥杂谈 1 前言 在日常的生活中&#xff0c;大家偶尔会看到朋友圈发的照片由一张被切成九张的效果&#xff0c;有时由一张照片被切成九张照片所带来的视觉盛宴是不一样的&#xff01; 现在许多 P 图工具里面自带了这种功能&#xff0c;而微信小程…

Android 九宫格布局(图片上传、预览)

前言&#xff1a; 利用RecyclerView展示朋友圈UI布局&#xff0c;包含展示、预览、删除等功能 效果图 1、在项目app\build.gradle添加依赖 //图片加载implementation com.github.bumptech.glide:glide:4.8.0//初始化控件找IDimplementation com.jakewharton:butterknife:10…

unity3d 中场景不显示了

应该是把Layers下面的Nothing打开了

python九宫格图片的原理~

原理 1、用Python制作的九宫格图像生成器包装exe文件&#xff0c;用户无需部署安装Python的开发环境&#xff0c;即可在当地运行该程序&#xff0c;快速生成九宫格图像。 2、用PIL库不断画小区域&#xff0c;切下来存储成新的小图片。 实例 假设每一个格子的宽和高分别是w、…

用纯css实现一个图片拼接九宫格

<style> body{ margin: 0; padding: 0; // 设定居中 display: flex; justify-content: center; align-items: center; height: 100vh; } .container{ width: 300px; height: 300px; display: flex; // 子盒子布局&#xff0c;要让子盒子之间有间隙就把宽高设大一些。 jus…

九宫格

一、定义 什么叫.9.PNG呢&#xff0c;这是安卓开发里面的一种特殊的图片 这种格式的图片在android 环境下具有自适应调节大小的能力。 &#xff08;1&#xff09;允许开发人员定义可扩展区域&#xff0c;当需要延伸图片以填充比图片本身更大区域时&#xff0c;可扩展区的内容被…

自定义九宫格图片

注&#xff1a;此Demo只是为了学习自定义相关知识&#xff0c;不建议在项目中引用使用. 已实现功能&#xff1a; 1.可设置每行显示的列数、删除图标、加号图片、设置padding&#xff0c;删除图标与图片的间距&#xff0c; 行和列的间距以及删除图标的大小; 2.match_parent模式…

基础复习——图形定制——图形Drawable——形状图形——九宫格图片——状态列表图形...

Drawable类型表达了各种各样的图形&#xff0c;包括图片、色块、画板、背景等。 包含图片在内的图形文件放在res目录的各个drawable目录下&#xff0c;其中drawable目录一般保存描述性的XML文件&#xff0c;而图片文件一般放在具体分辨率的drawable目录下。 各视图的background…

html用九张图片做出九宫图,用ps如何将九张照片做成九宫格?

如何用PS将九张照片做成九宫格&#xff0c;并且随时可以更换呢。下面跟搞设计一起来做一下吧。 ↑ 首先准备好九张照片 ↑ 打开PS&#xff0c;新建一个1000x1000的文件 ↑ 选择矩形工具 ↑ 创建一个300x300的矩形 ↑ 随便填个颜色&#xff0c;把图层命名为1 ↑ 选择视图-新建参…

Grid 布局实现九宫格图片动画

前言 &#x1f44f;Grid 布局实现九宫格&#xff0c;background-position设置背景图像起始位置&#xff0c;速速来Get吧~ &#x1f947;文末分享源代码。记得点赞关注收藏&#xff01; 1.实现效果 2.实现步骤 定义css变量&#xff1a;九宫格中每个宫格的长/宽为w&#xff0c…

九宫格拼图

九宫格拼图的实现&#xff1a; 效果为下图所示&#xff0c;实现拼图的完成&#xff0c;这里附上完整的代码&#xff0c;需要注意的是在产生随机的初始状态时&#xff0c;是随机两两交换&#xff0c;才能保证其一直是1-9的这样的情况&#xff0c;其他具体响应的设计在程序注释中…

【Android】-- 图形(形状图形、状态列表图形、九宫格图片)

一、图形Drawable Drawable类型表达了各种各样的图形&#xff0c;包括图片、色块、画板、背景等。 包含图片在内的图形文件放在res目录的各个drawable目录下&#xff0c;其中drawable目录一般保存描述性的XML文件&#xff0c;而图片文件一般放在具体分辨率的drawable目录下。…

Python生成九宫格图片

一、前言 大家在朋友圈应该看到过用一张图片以九宫格的方式显示&#xff0c;效果大致如下&#xff1a; 要实现上面的效果非常简单&#xff0c;我们只需要截取图片的九个区域即可。今天我们就要带大家使用Python来实现一下九宫格图片的生成。在开始之前&#xff0c;我们需要安…

【设计模式】策略模式

【设计模式】策略模式 1.什么是策略模式2.&#x1f330;一整坨代码实现策略模式重构代码 附录 1.什么是策略模式 策略模式是一种行为设计模式&#xff0c; 它能让你定义一系列算法&#xff0c; 并将每种算法分别放入独立的类中&#xff0c; 以使算法的对象能够相互替换。 2.&…

520表白浪漫的句子文案用便签记下来

在5月20日也就是520这个美好的情人节里&#xff0c;相信有很多年轻的情侣们都要出来约会了&#xff0c;一起度过这个浪漫的节日。并且520这个节日也是非常适合向心仪的人表白的哦&#xff0c;其实有不少小伙伴都是想要向某个人表白的&#xff0c;但是当面表白又不太好意思&…

文艺-浪漫句子

宇宙级别的浪漫 这朵世间最美好的玫瑰&#xff0c;星尘为泥&#xff0c;银河滋养。永远不会枯萎&#xff0c;永远在沉静宇宙中盛放。这是我要给你的&#xff0c;宇宙级别的浪漫——自珩给习清看玫瑰星云的时候说 当丁达尔效应出现的时候光就有了形状&#xff0c;这是我要给你…

HACKER KID: 1.0.1实战演练

文章目录 HACKER KID: 1.0.1实战演练一、前期准备1、相关信息 二、信息收集1、端口扫描2、访问网站3、扫描目录4、查看源码5、请求参数6、burpsuite批量请求7、编辑hosts文件8、DNS区域传输9、编辑hosts10、访问网站11、注册账号12、burpsuite抓包13、XML注入14、解密15、登录网…

Android系统中的Binder通信机制分析(6)- Binder通信机制详解

声明 其实对于Android系统Binder通信的机制早就有分析的想法&#xff0c;记得2019年6、7月份Mr.Deng离职期间约定一起对其进行研究的&#xff0c;但因为我个人问题没能实施这个计划&#xff0c;留下些许遗憾…文中参考了很多书籍及博客内容&#xff0c;可能涉及的比较多先不具…

Linux I2C驱动分析4 - GPIO模拟I2C

一. 前言 在嵌入式开发中&#xff0c;由于芯片的I2C接口有限&#xff0c;或者出于硬件画板的方便&#xff0c;我们都需要将普通的GPIO模拟I2C接口使用。出于对这样的需求&#xff0c;Linux-2.6.x已经有相关代码了&#xff0c;Linux-3.x有标准的内核选项支持该功能&#xff0c;内…