时间:2021-05-19
本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
//网络权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" />//添加依赖
compile 'com.android.support:appcompat-v7:26.+'compile 'com.android.support.constraint:constraint-layout:1.0.2'compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'compile 'com.facebook.fresco:fresco:0.12.0'testCompile 'junit:junit:4.12'布局文件
activity_main
<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="com.spl.dell.fresco_photowall.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/main_recycler" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></RelativeLayout>activity_zoom_image
<RelativeLayout xmlns:fresco="http://schemas.android.com/apk/res-auto" 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="com.spl.dell.fresco_photowall.ZoomImageActivity"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img_sdv" android:background="@mipmap/ic_launcher" android:layout_centerInParent="true" android:layout_width="400dp" android:layout_height="400dp" /></RelativeLayout>fresco_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv" android:layout_marginLeft="35dp" android:layout_width="100dp" android:layout_height="100dp" fresco:placeholderImage="@drawable/icon_placeholder" fresco:placeholderImageScaleType="fitCenter" fresco:progressBarImage="@drawable/icon_progress_bar" fresco:progressBarImageScaleType="centerInside" fresco:progressBarAutoRotateInterval="5000" fresco:failureImage="@drawable/icon_failure" fresco:failureImageScaleType="centerInside" fresco:retryImage="@drawable/icon_retry" fresco:retryImageScaleType="centerCrop" /></LinearLayout>MyViewHolder
import android.support.v7.widget.RecyclerView;import android.view.View;import com.facebook.drawee.view.SimpleDraweeView;import com.spl.dell.fresco_photowall.R;/** * Created by dell on 2018/3/8. */public class MyViewHolder extends RecyclerView.ViewHolder { public SimpleDraweeView simpleDraweeView; public MyViewHolder(View itemView) { super(itemView); simpleDraweeView = itemView.findViewById(R.id.sdv); }}MyRecyclerViewAdapter
import android.content.Context;import android.net.Uri;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import com.spl.dell.fresco_photowall.R;import com.spl.dell.fresco_photowall.recyclerview_adapter.viewholder.MyViewHolder;import java.io.File;import java.util.List;/** * Created by dell on 2018/3/8. */public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder> { Context context; List<String> list; public MyRecyclerViewAdapter(Context context, List<String> list) { this.context = context; this.list = list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.fresco_item, null); MyViewHolder viewHolder = new MyViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { Uri uri = Uri.fromFile(new File(list.get(position))); holder.simpleDraweeView.setImageURI(uri); holder.simpleDraweeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onRecyclerItemClick.onItemClicklistener(position); } }); } public void setOnItemClick(onRecyclerItemClick onRecyclerItemClick){ this.onRecyclerItemClick = onRecyclerItemClick; } @Override public int getItemCount() { return list.size(); } onRecyclerItemClick onRecyclerItemClick; public interface onRecyclerItemClick{ void onItemClicklistener(int position); }}MyApp
import android.app.Application;import com.facebook.drawee.backends.pipeline.Fresco;/** * Created by dell on 2018/3/8. */public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); }}ZoomImageActivity
import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.facebook.drawee.view.SimpleDraweeView;import java.io.File;public class ZoomImageActivity extends AppCompatActivity { private SimpleDraweeView sdv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_zoom_image); Intent intent = getIntent(); String imgurl = intent.getStringExtra("imgurl"); Uri uri = Uri.fromFile(new File(imgurl)); sdv = (SimpleDraweeView) findViewById(R.id.img_sdv); sdv.setImageURI(uri); }}MainActivity
import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.GridLayoutManager;import android.support.v7.widget.OrientationHelper;import android.support.v7.widget.RecyclerView;import com.spl.dell.fresco_photowall.recyclerview_adapter.MyRecyclerViewAdapter;import java.io.File;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private RecyclerView recycler; private List<String> docs = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_main); initView(); //recyclerview的数据源需要从手机相册中获取 initData(); } private void initData() { Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null); while(cursor.moveToNext()){ String paths = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); File file = new File(paths); String absolutePath = file.getAbsolutePath(); docs.add(absolutePath); } cursor.close();// for(int i = 0;i<docs.size();i++){// Log.d("TAG",docs.get(i).toString());// } runOnUiThread(new Runnable() { @Override public void run() { MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(MainActivity.this,docs); recycler.setAdapter(adapter); adapter.setOnItemClick(new MyRecyclerViewAdapter.onRecyclerItemClick() { @Override public void onItemClicklistener(int position) { Intent intent = new Intent(MainActivity.this,ZoomImageActivity.class); intent.putExtra("imgurl",docs.get(position)); startActivity(intent); } }); } }); } private void initView() { recycler = (RecyclerView) findViewById(R.id.main_recycler); RecyclerView.LayoutManager manager = new GridLayoutManager(MainActivity.this,3 ,OrientationHelper.VERTICAL,false); recycler.setLayoutManager(manager); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
当前较为知名的几个图片加载库是Universal-ImageLoader、Glide、Fresco、Picasso比较如下:Universal-ImageLoa
优化背景一般情况下,Fresco图片加载需使用SimpleDraweeView,这个控件并不能自动根据自身的尺寸按需加载图片,即一个N×N的UI控件,背后加载的
在微信小程序中,要显示一张图片,有两种图片加载方式:加载本地图片加载网络图片加载本地图片src="/image/arrowright.png"这句就是加载本地图
页面滚动动态加载数据,页面下拉自动加载内容相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多有手机的相信都见过
项目简介:该项目为加载大图片详细介绍:对于超大的图片,如果不缩放的话,容易导致内存溢出。而经过处理后,无论多大的图片,都能够在手机屏幕上加载出来,不会导致内存溢