本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)是一家專業(yè)提供鳳山企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為鳳山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
//網(wǎng)絡(luò)權(quán)限
<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的數(shù)據(jù)源需要從手機(jī)相冊中獲取 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); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
標(biāo)題名稱:Fresco加載手機(jī)圖片墻
URL標(biāo)題:http://muchs.cn/article6/ihdiig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、建站公司、網(wǎng)站改版、關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)