Android實(shí)現(xiàn)圖片九宮格
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圖片九宮格的具體代碼,供大家參考,具體內(nèi)容如下
九宮格分三類

實(shí)現(xiàn)的效果






具體實(shí)現(xiàn)
activity_main
<?xml version="1.0" encoding="utf-8"?> <layout 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"> ? ?<data> ? ?</data> ? ? <androidx.constraintlayout.widget.ConstraintLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent"> ? ? ? ? <androidx.recyclerview.widget.RecyclerView ? ? ? ? ? ? android:id="@+id/recyclerView" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? app:layout_constraintBottom_toBottomOf="parent" ? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent" ? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent" ? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" /> ? ? </androidx.constraintlayout.widget.ConstraintLayout> </layout>
item_main
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto">
? ? <data>
? ? ? ? <variable
? ? ? ? ? ? name="img"
? ? ? ? ? ? type="com.nooneb.ninegrid.Img" />
? ? ? ? <import type="android.view.View"/>
? ? </data>
? ? <androidx.constraintlayout.widget.ConstraintLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:padding="64dp">
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/oneImg"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOne()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintHorizontal_bias="0.0"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent"
? ? ? ? ? ? app:localImg="@{img.img1}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/twoImg1"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isTwoOrFour()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="w,1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline2"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent"
? ? ? ? ? ? app:localImg="@{img.img1}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/twoImg2"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isTwoOrFour()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline2"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent"
? ? ? ? ? ? app:localImg="@{img.img2}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/twoImg3"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isFour()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="w,1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline2"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/twoImg1"
? ? ? ? ? ? app:localImg="@{img.img3}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/twoImg4"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isFour()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline2"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/twoImg2"
? ? ? ? ? ? app:localImg="@{img.img4}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg1"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="w,1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline3"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent"
? ? ? ? ? ? app:localImg="@{img.img1}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg2"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline4"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline3"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent"
? ? ? ? ? ? app:localImg="@{img.img2}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg3"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline4"
? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent"
? ? ? ? ? ? app:localImg="@{img.img3}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg4"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline3"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/threeImg1"
? ? ? ? ? ? app:localImg="@{img.img4}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg5"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline4"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline3"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/threeImg2"
? ? ? ? ? ? app:localImg="@{img.img5}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg7"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline3"
? ? ? ? ? ? app:layout_constraintHorizontal_bias="0.0"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/threeImg4"
? ? ? ? ? ? app:localImg="@{img.img7}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg8"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline4"
? ? ? ? ? ? app:layout_constraintHorizontal_bias="0.0"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline3"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/threeImg5"
? ? ? ? ? ? app:localImg="@{img.img8}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg6"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline4"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/threeImg3"
? ? ? ? ? ? app:localImg="@{img.img6}" />
? ? ? ? <ImageView
? ? ? ? ? ? android:id="@+id/threeImg9"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="0dp"
? ? ? ? ? ? android:visibility="@{img.isOther()?View.VISIBLE:View.GONE}"
? ? ? ? ? ? app:layout_constraintDimensionRatio="1:1"
? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? ? ? app:layout_constraintHorizontal_bias="0.0"
? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline4"
? ? ? ? ? ? app:layout_constraintTop_toBottomOf="@+id/threeImg6"
? ? ? ? ? ? app:localImg="@{img.img9}" />
? ? ? ? <androidx.constraintlayout.widget.Guideline
? ? ? ? ? ? android:id="@+id/guideline2"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:orientation="vertical"
? ? ? ? ? ? app:layout_constraintGuide_percent=".5" />
? ? ? ? <androidx.constraintlayout.widget.Guideline
? ? ? ? ? ? android:id="@+id/guideline3"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:orientation="vertical"
? ? ? ? ? ? app:layout_constraintGuide_percent=".333333" />
? ? ? ? <androidx.constraintlayout.widget.Guideline
? ? ? ? ? ? android:id="@+id/guideline4"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:orientation="vertical"
? ? ? ? ? ? app:layout_constraintGuide_percent=".666666" />
? ? </androidx.constraintlayout.widget.ConstraintLayout>
</layout>實(shí)體類
public class Img {
? ? public Integer img1;
? ? public Integer img2;
? ? public Integer img3;
? ? public Integer img4;
? ? public Integer img5;
? ? public Integer img6;
? ? public Integer img7;
? ? public Integer img8;
? ? public Integer img9;
? ? public Img(Integer img1, Integer img2, Integer img3, Integer img4, Integer img5, Integer img6, Integer img7, Integer img8, Integer img9) {
? ? ? ? this.img1 = img1;
? ? ? ? this.img2 = img2;
? ? ? ? this.img3 = img3;
? ? ? ? this.img4 = img4;
? ? ? ? this.img5 = img5;
? ? ? ? this.img6 = img6;
? ? ? ? this.img7 = img7;
? ? ? ? this.img8 = img8;
? ? ? ? this.img9 = img9;
? ? }
? ? public int count(){
? ? ? ? int i=0;
? ? ? ? if (img1!=null)i++;
? ? ? ? if (img2!=null)i++;
? ? ? ? if (img3!=null)i++;
? ? ? ? if (img4!=null)i++;
? ? ? ? if (img5!=null)i++;
? ? ? ? if (img6!=null)i++;
? ? ? ? if (img7!=null)i++;
? ? ? ? if (img8!=null)i++;
? ? ? ? if (img9!=null)i++;
? ? ? ? return i;
? ? }
? ? public boolean isOne(){
? ? ? ? return count()==1;
? ? }
? ? public boolean isTwoOrFour(){
? ? ? ? return count()==2||count()==4;
? ? }
? ? public boolean isFour(){
? ? ? ? return count()==4;
? ? }
? ? public boolean isOther(){
? ? ? ? if (count()!=1){
? ? ? ? ? ? if (count()!=2){
? ? ? ? ? ? ? ? return count() != 4;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
}圖片適配器
public class ImgAdapter {
? ? @BindingAdapter("localImg")
? ? public static void set(ImageView imageView,Integer res){
? ? ? ? if (res==null){
? ? ? ? ? ? imageView.setVisibility(View.GONE);
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? imageView.setImageResource(res);
? ? }
}列表適配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {
? ? private final Context context;
? ? public List<Img> imgs;
? ? public MyAdapter(Context context,List<Img> imgs) {
? ? ? ? this.context = context;
? ? ? ? this.imgs=imgs;
? ? }
? ? @NonNull
? ? @Override
? ? public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
? ? ? ? ItemImgBinding binding = ItemImgBinding.inflate(
? ? ? ? ? ? ? ? LayoutInflater.from(context),
? ? ? ? ? ? ? ? parent,
? ? ? ? ? ? ? ? false);
? ? ? ? return new Holder(binding);
? ? }
? ? @Override
? ? public void onBindViewHolder(@NonNull Holder holder, int position) {
? ? ? ? Img img = imgs.get(position);
? ? ? ? holder.binding.setImg(img);
? ? ? ? holder.binding.executePendingBindings();
? ? }
? ? @Override
? ? public int getItemViewType(int position) {
? ? ? ? return position;
? ? }
? ? @Override
? ? public int getItemCount() {
? ? ? ? return imgs.size();
? ? }
? ? public class Holder extends RecyclerView.ViewHolder {
? ? ? ? ItemImgBinding binding;
? ? ? ? public Holder(ItemImgBinding binding) {
? ? ? ? ? ? super(binding.getRoot());
? ? ? ? ? ? this.binding=binding;
? ? ? ? }
? ? }
}MainActivity
public class MainActivity extends AppCompatActivity {
? ? ActivityMainBinding binding;
? ? MyAdapter myAdapter;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? binding = ActivityMainBinding.inflate(getLayoutInflater());
? ? ? ? setContentView(binding.getRoot());
? ? ? ? List<Img> imgs = Arrays.asList(
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, null, null, null, null, null, null, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, null, null, null, null, null, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, null, null, null, null, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, R.drawable.avatar_4, null, null, null, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, R.drawable.avatar_4, R.drawable.avatar_5, null, null, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, R.drawable.avatar_4, R.drawable.avatar_5, R.drawable.avatar_6, null, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, R.drawable.avatar_4, R.drawable.avatar_5, R.drawable.avatar_6, R.drawable.avatar_7, null, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, R.drawable.avatar_4, R.drawable.avatar_5, R.drawable.avatar_6, R.drawable.avatar_7, R.drawable.avatar_8, null),
? ? ? ? ? ? ? ? new Img(R.drawable.avatar_1, R.drawable.avatar_2, R.drawable.avatar_3, R.drawable.avatar_4, R.drawable.avatar_5, R.drawable.avatar_6, R.drawable.avatar_7, R.drawable.avatar_8, R.drawable.avatar_9)
? ? ? ? );
? ? ? ? myAdapter=new MyAdapter(this,imgs);
? ? ? ? binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
? ? ? ? binding.recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
? ? ? ? binding.recyclerView.setAdapter(myAdapter);
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android不壓縮圖片實(shí)現(xiàn)高清加載巨圖實(shí)例
- Android實(shí)現(xiàn)手勢劃定區(qū)域裁剪圖片
- AndroidStudio實(shí)現(xiàn)能在圖片上涂鴉程序
- Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片
- Android實(shí)現(xiàn)左右滑動(dòng)切換圖片
- Android開發(fā)實(shí)現(xiàn)圖片大小與質(zhì)量壓縮及保存
- Android性能優(yōu)化之圖片大小,尺寸壓縮綜合解決方案
- Android實(shí)現(xiàn)圖片預(yù)覽與保存功能
- Android自定義視圖中圖片的處理
相關(guān)文章
Android ListView下拉刷新上拉自動(dòng)加載更多DEMO示例
這篇文章主要介紹了Android ListView下拉刷新上拉自動(dòng)加載更多DEMO示例的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android WebView的使用方法及與JS 相互調(diào)用
這篇文章主要介紹了Android WebView的使用方法及與JS 相互調(diào)用的相關(guān)資料,WebView 是 Android 中一個(gè)非常實(shí)用的組​件, WebView 可以使得網(wǎng)頁輕松的內(nèi)嵌到app里,還可以直接跟js相互調(diào)用,需要的朋友可以參考下2017-07-07
Android編程開發(fā)之NotiFication用法詳解
這篇文章主要介紹了Android編程開發(fā)之NotiFication用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了NotiFication的功能、使用技巧與注意事項(xiàng),需要的朋友可以參考下2015-12-12
Android開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android電話撥號(hào)器和短信發(fā)送器的具體原理與實(shí)現(xiàn)步驟,需要的朋友可以參考下2015-12-12
Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解
這篇文章主要為大家介紹了Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟
這篇文章主要介紹了Android Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02

