如何制作一个可绘制的一幅图像数组?

时间:2018-11-16 21:35:53

标签: android performance android-studio gridview android-drawable

我有这张图片:

 int lettersIcon[] ={
    R.drawable.ico_x
};

我想在矩阵/网格视图(NxM)上重复一次。

我对Android不太了解。

1 个答案:

答案 0 :(得分:0)

尝试此代码。

MainActivity:

package com.example.picassoapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class MainActivity extends Activity {

   GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = findViewById(R.id.grid);
        GridAdapter adapter= new GridAdapter(R.drawable.cute_cat);
        gridView.setAdapter(adapter);
    }

}

activity_main.xml:

  <?xml version="1.0" encoding="utf-8"?>
<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"
    >
    <GridView
        android:numColumns="3"
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></GridView>
</RelativeLayout>

GridAdapter:

    package com.example.picassoapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class GridAdapter extends BaseAdapter {
    int images;
    public GridAdapter(int images) {
        this.images= images;
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return images;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.grid_adapter, null);
        ImageView imageView= view.findViewById(R.id.imageView);
        imageView.setImageResource(images);
        return view;
    }
}

grid_adapter.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>