Advertisement
Guest User

UniPix_PixView

a guest
Mar 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.19 KB | None | 0 0
  1. package com.creetail.unipix;
  2. import android.view.*;
  3. import android.content.*;
  4. import android.graphics.*;
  5. import android.util.*;
  6. import android.widget.*;
  7. import java.util.*;
  8. import android.support.annotation.*;
  9.  
  10. public class PixView extends View
  11. {
  12.     private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG),
  13.         border = new Paint(Paint.ANTI_ALIAS_FLAG),
  14.         figures = new Paint();
  15.     private int nowColor = Color.TRANSPARENT;
  16.     public boolean grid = true, editMode = true, ffmode = false;
  17.     private TouchListener tl;
  18.     int width = 10, height = 10;
  19.     private int[] lastPix = new int[4];
  20.     private Bitmap bm, gridBm;
  21.     private Rect src, dest;
  22.     int figureType = 0;
  23.     private Bitmap fgBm;
  24.    
  25.     public PixView(Context c, AttributeSet attr){
  26.         super(c, attr);
  27.        
  28.         paint.setStyle(Paint.Style.FILL);
  29.         figures.setStyle(Paint.Style.STROKE);
  30.         figures.setColor(Color.BLACK);
  31.         figures.setAntiAlias(false);
  32.  
  33.         border.setStyle(Paint.Style.STROKE);
  34.         border.setColor(Color.BLACK);
  35.        
  36.         create(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
  37.     }
  38.    
  39.     public void create(Bitmap newBm){
  40.         width = newBm.getWidth();
  41.         height = newBm.getHeight();
  42.         bm = newBm;
  43.         src = new Rect(0, 0, width, height);
  44.         dest = new Rect(0, 0, width * getPixelSize(), height * getPixelSize());
  45.         gridBm = gridBm();
  46.        
  47.         Layers.layers = new ArrayList<Layers.Layer>();
  48.         Layers.Layer l = new Layers.Layer();
  49.         l.bm = bm;
  50.         l.hidden = false;
  51.         Layers.layers.add(l);
  52.         Layers.nowBitmap = bm;
  53.  
  54.         setOnTouchListener(new OnTouchListener(){
  55.                 public boolean onTouch(View v, MotionEvent m){
  56.                     if (Mode.currentMode != Mode.Modes.MOVE_MODE){
  57.                         int x = (int)m.getX(), y = (int)m.getY();
  58.  
  59.                         if (x < 0) x = 0;
  60.                         if (y < 0) y = 0;
  61.  
  62.                         int iX = x / getPixelSize(),
  63.                             iY = y / getPixelSize();
  64.  
  65.                         if (iX > width - 1) iX = width - 1;
  66.                         if (iY > height - 1) iY = height - 1;
  67.                            
  68.                         if (m.getAction() == m.ACTION_DOWN || m.getAction() == m.ACTION_MOVE){
  69.                             if (Mode.currentMode == Mode.Modes.DRAW_MODE){
  70.                                 Layers.nowBitmap.setPixel(iX, iY, nowColor);
  71.                             }else if (Mode.currentMode == Mode.Modes.RUBBER_MODE){
  72.                                 Layers.nowBitmap.setPixel(iX, iY, Color.TRANSPARENT);
  73.                             }else if (Mode.currentMode == Mode.Modes.FLOODFILL_MODE){
  74.                                 floodFill(Layers.nowBitmap, new Point(iX, iY), Layers.nowBitmap.getPixel(iX, iY), nowColor);
  75.                             }else if (Mode.currentMode == Mode.Modes.PICK_MODE){
  76.                                 MainActivity.getCurrentColorView().setColor(Layers.nowBitmap.getPixel(iX, iY));
  77.                                 setColor(Layers.nowBitmap.getPixel(iX, iY));
  78.                             }
  79.                             invalidate();
  80.                         }
  81.                        
  82.                         if (Mode.currentMode == Mode.Modes.FIGURE_MODE){
  83.                             if (m.getAction() == m.ACTION_DOWN){
  84.                                 lastPix[0] = iX;
  85.                                 lastPix[1] = iY;
  86.                                 lastPix[2] = iX;
  87.                                 lastPix[3] = iY + 1;
  88.                             }else if (m.getAction() == m.ACTION_MOVE){
  89.                                 lastPix[2] = iX;
  90.                                 lastPix[3] = iY + 1;
  91.                             }else if (m.getAction() == m.ACTION_UP){
  92.                                 Canvas c = new Canvas(Layers.nowBitmap);
  93.                                 c.drawLine(lastPix[0], lastPix[1], lastPix[2], lastPix[3], figures);
  94.                             }
  95.                            
  96.                             invalidate();
  97.                         }
  98.                     }
  99.                     tl.onTouch(m);
  100.                     return true;
  101.                 }
  102.             });
  103.     }
  104.    
  105.     public void newField(Bitmap newBm){
  106.         create(newBm);
  107.         invalidate();
  108.     }
  109.    
  110.     public Bitmap getBitmap (){
  111.         return bm;
  112.     }
  113.    
  114.     public void setTouchListener (TouchListener tl){
  115.         this.tl = tl;
  116.     }
  117.    
  118.     public void setColor (int color){
  119.         nowColor = color;
  120.         figures.setColor(color);
  121.     }
  122.  
  123.     private Bitmap gridBm(){
  124.         Bitmap b = Bitmap.createBitmap(width * getPixelSize(), height * getPixelSize(), Bitmap.Config.ARGB_8888);
  125.         Canvas c = new Canvas(b);
  126.         Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
  127.         p.setColor(Color.BLACK);
  128.         p.setStyle(Paint.Style.STROKE);
  129.         for (int x = 0; x < width; x++){
  130.             for (int y = 0; y < height; y++){
  131.                 int xx = x * getPixelSize(),
  132.                     yy = y * getPixelSize();
  133.                 Rect r = new Rect(xx, yy, xx + getPixelSize(), yy + getPixelSize());
  134.                 c.drawRect(r, p);
  135.             }
  136.         }
  137.         return b;
  138.     }
  139.    
  140.     public void floodFill(Bitmap image, Point node, int targetColor, int replacementColor) {
  141.         int width = image.getWidth();
  142.         int height = image.getHeight();
  143.         int target = targetColor;
  144.         int replacement = replacementColor;
  145.         if (target != replacement) {
  146.             Deque<Point> queue = new LinkedList<Point>();
  147.             do {
  148.                 int x = node.x;
  149.                 int y = node.y;
  150.                 while (x > 0 && image.getPixel(x - 1, y) == target) {
  151.                     x--;
  152.                 }
  153.                 boolean spanUp = false;
  154.                 boolean spanDown = false;
  155.                 while (x < width && image.getPixel(x, y) == target) {
  156.                     image.setPixel(x, y, replacement);
  157.                     if (!spanUp && y > 0 && image.getPixel(x, y - 1) == target) {
  158.                         queue.add(new Point(x, y - 1));
  159.                         spanUp = true;
  160.                     } else if (spanUp && y > 0 && image.getPixel(x, y - 1) != target) {
  161.                         spanUp = false;
  162.                     }
  163.                     if (!spanDown && y < height - 1 && image.getPixel(x, y + 1) == target) {
  164.                         queue.add(new Point(x, y + 1));
  165.                         spanDown = true;
  166.                     } else if (spanDown && y < height - 1 && image.getPixel(x, y + 1) != target) {
  167.                         spanDown = false;
  168.                     }
  169.                     x++;
  170.                 }
  171.             } while ((node = queue.pollFirst()) != null);
  172.         }
  173.     }
  174.    
  175.     public int getPixelSize (){
  176.         DisplayMetrics d = getResources().getDisplayMetrics();
  177.         return Math.min (d.widthPixels, d.heightPixels) / Math.max(width, height);
  178.     }
  179.    
  180.     @Override
  181.     public void onDraw(Canvas can){
  182.         super.onDraw(can);
  183.         for (int i = Layers.layers.size() != 0?Layers.layers.size() - 1 : 0; i >= 0; i--){
  184.             Layers.Layer layer = Layers.layers.get(i);
  185.             if (!layer.hidden){
  186.                 paint.setAlpha(layer.alpha);
  187.                 can.drawBitmap(layer.bm, src, dest, paint);
  188.             }
  189.         }
  190.         can.drawLine(lastPix[0], lastPix[1], lastPix[2], lastPix[3], figures);
  191.         if (grid) can.drawBitmap(gridBm, 0, 0, border);
  192.     }
  193.  
  194.     @Override
  195.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  196.     {
  197.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  198.         setMeasuredDimension(width * getPixelSize(), height * getPixelSize());
  199.     }
  200.    
  201.     public void setEditMode (boolean editMode){
  202.         this.editMode = editMode;
  203.     }
  204.    
  205.     public void grid (boolean bool){
  206.         this.grid = bool;
  207.         invalidate();
  208.     }
  209.  
  210.     public boolean grid (){
  211.         return grid;
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement