Android 如何绘制矩形对象的网格?
我是Android新手,我仍在寻找好的资源 我的问题涉及到在屏幕上绘制矩形网格的最佳方法。我需要网格中显示的每个对象都有一个初始文本(或int作为文本)值,当用户触摸该对象时,文本将变为彩色形状。此外,每个对象都需要知道(或能够发现)其近邻的状态 我不知道要扩展哪个类才能同时显示文本和形状,并且能够处理触摸输入 谢谢你的帮助 编辑:Android 如何绘制矩形对象的网格?,android,object,drawing,Android,Object,Drawing,我是Android新手,我仍在寻找好的资源 我的问题涉及到在屏幕上绘制矩形网格的最佳方法。我需要网格中显示的每个对象都有一个初始文本(或int作为文本)值,当用户触摸该对象时,文本将变为彩色形状。此外,每个对象都需要知道(或能够发现)其近邻的状态 我不知道要扩展哪个类才能同时显示文本和形状,并且能够处理触摸输入 谢谢你的帮助 编辑: 我很抱歉,但我不知道怎么说得更清楚。也许一些背景知识会有所帮助。我有一个主活动,它接受一个int值作为输入,并创建一个将该值发送给另一个活动的意图。然后,另一个活动
我很抱歉,但我不知道怎么说得更清楚。也许一些背景知识会有所帮助。我有一个主活动,它接受一个int值作为输入,并创建一个将该值发送给另一个活动的意图。然后,另一个活动将显示一个由100个随机数组成的网格。用户需要选择一系列网格点,并且用户选择的一定数量的网格点将从随机数更改为彩色形状。变化点由我将在代码中提供的逻辑控制。因此,使用基本android图形: Android自定义控件官方指南 视图控件上的Android参考页,您将在其上展开 自定义视图控件的真实代码示例(您特别感兴趣的是
onDraw()
)
SO Q与基本android图形: Android自定义控件官方指南 视图控件上的Android参考页,您将在其上展开 自定义视图控件的真实代码示例(您特别感兴趣的是
onDraw()
)
我知道这个问题问了很久了,但也许会有帮助,ppl:) 将此项添加到“attrs.xml”(或根据需要创建新项)
这是一个类-“RectanglesGridView.java”
package com.gilapps.movinglivewallper.UI.views;
导入android.annotation.TargetApi;
导入android.content.Context;
导入android.content.res.Resources;
导入android.content.res.TypedArray;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.os.Build;
导入android.util.AttributeSet;
导入android.util.DisplayMetrics;
导入android.view.view;
导入com.gilapps.movingLiveWallper.R;
公共类矩形RIDVIEW扩展视图{
专用最终静态浮动默认单元格大小DP=10;
私有最终静态int默认_单元格_COLOR1=Color.GRAY;
私有最终静态int DEFAULT_CELL_COLOR2=Color.WHITE;
private int mColor1=默认的单元格颜色1;
private int mColor2=默认的单元格颜色2;
私人浮动mCellSize;
私人油漆;
私有布尔混合色1;
私人英特姆维兹;
私营机构;
公共矩形栅格视图(上下文){
超级(上下文);
mCellSize=convertDpToPixel(默认单元格大小);
mPaint=新油漆();
}
公共矩形栅格视图(上下文、属性集属性){
超级(上下文,attrs);
mPaint=新油漆();
加载属性(上下文、属性);
}
公共矩形RIDVIEW(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
mPaint=新油漆();
加载属性(上下文、属性);
}
@TargetApi(Build.VERSION\u code.LOLLIPOP)
公共矩形RIDVIEW(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
mPaint=新油漆();
加载属性(上下文、属性);
}
私有void加载属性(上下文上下文、属性集属性){
TypedArray a=context.getTheme().ActainStyledAttributes(
属性,
R.styleable.RectanglesGridView,
0, 0);
试一试{
mCellSize=a.getDimension(R.styleable.RectanglesGridView_cellSize,convertDpToPixel(默认值_CELL_SIZE_DP));
mColor1=a.getColor(R.styleable.RectanglesGridView\u cellColor1,默认值\u CELL\u COLOR1);
mColor2=a.getColor(R.styleable.RectanglesGridView\u cellColor2,默认值\u CELL\u COLOR2);
}捕获(例外e){
mCellSize=convertDpToPixel(默认单元格大小);
}最后{
a、 回收();
}
}
专用浮点转换器DPtopixel(浮点dp){
Resources=getContext().getResources();
DisplayMetrics=resources.getDisplayMetrics();
float px=dp*((float)metrics.densityDpi/DisplayMetrics.DENSITY\u默认值);
返回px;
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
mWidth=w;
mHeight=h;
super.onSizeChanged(w,h,oldw,oldh);
}
@凌驾
受保护的void onDraw(画布){
对于(float r=0;r我知道这个问题问了很长时间,但可能对ppl有帮助:)
将此项添加到“attrs.xml”(或根据需要创建新项)
这是一个类-“RectanglesGridView.java”
package com.gilapps.movinglivewallper.UI.views;
导入android.annotation.TargetApi;
导入android.content.Context;
导入android.content.res.Resources;
导入android.content.res.TypedArray;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.os.Build;
导入android.util.AttributeSet;
导入android.util.DisplayMetrics;
导入android.view.view;
导入com.gilapps.movingLiveWallper.R;
公共类矩形RIDVIEW扩展视图{
专用最终静态浮动默认单元格大小DP=10;
私有最终静态int默认_单元格_COLOR1=Color.GRAY;
私有最终静态整数默认值
<resources>
<declare-styleable name="RectanglesGridView">
<attr name="cellSize" format="dimension" />
<attr name="cellColor1" format="color" />
<attr name="cellColor2" format="color" />
</declare-styleable>
package com.gilapps.movinglivewallpaper.UI.views;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import com.gilapps.movinglivewallpaper.R;
public class RectanglesGridView extends View {
private final static float DEFAULT_CELL_SIZE_DP = 10;
private final static int DEFAULT_CELL_COLOR1 = Color.GRAY;
private final static int DEFAULT_CELL_COLOR2 = Color.WHITE;
private int mColor1 = DEFAULT_CELL_COLOR1;
private int mColor2 = DEFAULT_CELL_COLOR2;
private float mCellSize;
private Paint mPaint;
private boolean mIsColor1;
private int mWidth;
private int mHeight;
public RectanglesGridView(Context context) {
super(context);
mCellSize = convertDpToPixel(DEFAULT_CELL_SIZE_DP);
mPaint = new Paint();
}
public RectanglesGridView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
loadAttributes(context, attrs);
}
public RectanglesGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
loadAttributes(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RectanglesGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mPaint = new Paint();
loadAttributes(context, attrs);
}
private void loadAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.RectanglesGridView,
0, 0);
try {
mCellSize = a.getDimension(R.styleable.RectanglesGridView_cellSize, convertDpToPixel(DEFAULT_CELL_SIZE_DP));
mColor1 = a.getColor(R.styleable.RectanglesGridView_cellColor1, DEFAULT_CELL_COLOR1);
mColor2 = a.getColor(R.styleable.RectanglesGridView_cellColor2, DEFAULT_CELL_COLOR2);
} catch (Exception e) {
mCellSize = convertDpToPixel(DEFAULT_CELL_SIZE_DP);
} finally {
a.recycle();
}
}
private float convertDpToPixel(float dp){
Resources resources = getContext().getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = w;
mHeight = h;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
for (float r=0;r<mHeight;r+=mCellSize) {
for (float c=0;c<mWidth;c+=mCellSize) {
mPaint.setColor(mIsColor1 ? mColor2 : mColor1);
mIsColor1 = !mIsColor1;
canvas.drawRect(c,r,c+mCellSize,r+mCellSize,mPaint);
}
mIsColor1 = !mIsColor1;
}
super.onDraw(canvas);
}
}
<com.gilapps.movinglivewallpaper.UI.views.RectanglesGridView
app:cellColor1="#33000000"
app:cellColor2="white"
app:cellSize="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />