问题描述
我对这一切都很陌生,我正在为孩子们创建一个应用程序,其中包括一个着色书类别。老实说,我找到了我不太明白的着色书的以下代码。但我面临一个问题,即点击时填写此代码。这意味着绘图的单击部分将填充所选颜色。但我的想法是通过移动手指来给图片上色(就像画画,但有颜色)但老实说我不知道该怎么做。
这是代码
绘本类
Public class ColoringBook extends AppCompatActivity {
RecyclerView recyclerView;
ImageAdapter adapter;
//creating variables
Button back;
Toolbar toolbar;
TextView toolText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coloring_book);
//Assign action bar
toolbar = findViewById(R.id.toolbar3);
setSupportActionBar(toolbar);
//assign TextView in Toolbar
toolText = findViewById(R.id.toolbar3_text);
//setText of TextView in Toolbar
toolText.setText("Coloring Book");
//Assign back button
back = findViewById(R.id.back);
//setonClickListener for back btn
back.setonClickListener(new View.OnClickListener() {
//onClick for back button
@Override
public void onClick(View v) {
Intent mainIntent = new Intent(ColoringBook.this,MainActivity.class);
startActivity(mainIntent);
}//end of onClick
});//end of setonClickListener
initView();
}
private void initView() {
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recyclerView = findViewById(R.id.recycle_view_images);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ImageAdapter(this);
recyclerView.setAdapter(adapter);
}
}
普通类
public class Common {
public static int COLOR_SELECTED = Color.RED;
public static int PICTURE_SELECTED;
}
FloodFill 类
public class FloodFill {
public static void floodFill(Bitmap bitmap,Point point,int targetColor,int newColor){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if(targetColor != newColor){
Queue<Point> queue = new LinkedList<>();
do{
int x = point.x;
int y = point.y;
while (x>0 && bitmap.getPixel(x-1,y)==targetColor){
x--;
}
boolean spanUp = false;
boolean spandDown = false;
while (x < width && bitmap.getPixel(x,y) == targetColor){
bitmap.setPixel(x,y,newColor);
if (!spanUp && y>0 && bitmap.getPixel(x,y-1)==targetColor){
queue.add(new Point(x,y-1));
spanUp = true;
}else if(spanUp && y>0 && bitmap.getPixel(x,y-1)==targetColor){
spanUp = false;
}
if(!spandDown && y<height-1 && bitmap.getPixel(x,y+1)==targetColor){
queue.add(new Point(x,y+1));
spandDown=true;
}else if (spandDown && y<height-1 && bitmap.getPixel(x,y+1)!=targetColor){
spandDown = false;
}
x++;
}
}while((point=queue.poll())!=null);
}
}
}
ImageAdapter 类
public class ImageAdapter extends RecyclerView.Adapter<ImageViewHolder> {
private Context mContext;
private List<Integer> listimages;
public ImageAdapter(Context mContext) {
this.mContext = mContext;
this.listimages = getimages();
}
private List<Integer> getimages() {
List<Integer> results = new ArrayList<>();
results.add(R.drawable.lion_coloring);
results.add(R.drawable.turtle_coloring);
results.add(R.drawable.leopard_coloring);
results.add(R.drawable.elephant_coloring);
results.add(R.drawable.shark_coloring);
return results;
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_images,parent,false);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder,int position) {
holder.imageView.setimageResource(listimages.get(position));
holder.setimageOnClick(new ImageOnClick() {
@Override
public void onClick(int pos) {
Common.PICTURE_SELECTED = listimages.get(pos);
mContext.startActivity(new Intent(mContext,PaintActivity.class));
}
});
}
@Override
public int getItemCount() {
return listimages.size();
}
}
ImageOnClick 接口
public interface ImageOnClick {
void onClick(int pos);
}
ImageViewHolder 类
public class ImageViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
private ImageOnClick imageOnClick;
public void setimageOnClick(ImageOnClick imageOnClick) {
this.imageOnClick = imageOnClick;
}
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_outline);
itemView.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageOnClick.onClick(getAdapterPosition());
}
});
}
}
绘画活动
public class PaintActivity extends AppCompatActivity implements SpectrumPalette.OnColorSelectedListener {
//creating variables
Button back;
Toolbar toolbar;
TextView toolText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint);
//Assign action bar
toolbar = findViewById(R.id.toolbar3);
setSupportActionBar(toolbar);
//assign TextView in Toolbar
toolText = findViewById(R.id.toolbar3_text);
//setText of TextView in Toolbar
toolText.setText("Paint");
//Assign back button
back = findViewById(R.id.back);
//setonClickListener for back btn
back.setonClickListener(new View.OnClickListener() {
//onClick for back button
@Override
public void onClick(View v) {
Intent mainIntent = new Intent(PaintActivity.this,ColoringBook.class);
startActivity(mainIntent);
}//end of onClick
});//end of setonClickListener
SpectrumPalette spectrumPalette = findViewById(R.id.palette);
spectrumPalette.setonColorSelectedListener(this);
}
@Override
public void onColorSelected(int color) {
Common.COLOR_SELECTED = color;
}
}
PaintView 类
public class PaintView extends View {
Bitmap bitmap;
public PaintView(Context context) {
super(context);
}
public PaintView(Context context,@Nullable AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onSizeChanged(int w,int h,int oldw,int oldh) {
super.onSizeChanged(w,h,oldw,oldh);
Bitmap srcBitmp = BitmapFactory.decodeResource(getResources(),Common.PICTURE_SELECTED);
bitmap = Bitmap.createScaledBitmap(srcBitmp,w,false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap,null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
paint((int)event.getX(),(int)event.getY());
return true;
}
private void paint(int x,int y) {
int targetColor = bitmap.getPixel(x,y);
FloodFill.floodFill(bitmap,new Point(x,y),targetColor,Common.COLOR_SELECTED);
invalidate();
}
}
请原谅我糟糕的英语。任何帮助或支持将不胜感激。
提前致谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)