Android 화판 개발 의 배경 추가 및 화판 내용 저장

5508 단어 Android화판
배경 그리 기
배경 을 그 리 는 방법 은 두 가지 가 있 습 니 다.
  • 자신 이 canvas 를 이용 하여 그립 니 다
  • view 의 자체 테이프 방법 으로 그립 니 다

  • 1.1 캔버스 배경 그리 기
    자신 이 그린 배경 을 그 리 는 방법 은 onDraw 리 셋 으로 그 릴 때 배경 을 그 려 서 원래 의 내용 을 그 리 는 것 입 니 다.
    
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
    
        //         
        canvas.drawColor(Color.BLACK)
    
        //    bitmap   
        canvas.drawBitmap(mBufferBitmap,0f,0f,null)
    
      }
    1.2 view 자체 테이프 방법
    view 는 setBackground 방법 이 있 습 니 다.저 는 이 방법 으로 배경 setBackground Resource(R.drawable.bg)를 설정 합 니 다.이 방법 이 어떻게 실행 되 는 지 소스 코드 를 간단하게 추적 하고 ctrl+마우스 왼쪽 단 추 를 누 르 면 이 방법 은 setBackgroundDrawable 로 넘 어 갑 니 다.다음은 소스 코드(삭제)입 니 다.
    
    public void setBackgroundDrawable(Drawable background) {
        computeOpaqueFlags();
    
        if (background == mBackground) {
          return;
        }
    
        boolean requestLayout = false;
    
        mBackgroundResource = 0;
        ......
        if (background != null) {
          ......
    
    
          mBackground = background;
    
    
          applyBackgroundTint();
    
          ...
        } else {
          /* Remove the background */
          mBackground = null;
          .......
        }
    
        computeOpaqueFlags();
    
        if (requestLayout) {
          requestLayout();
        }
    
        mBackgroundSizeChanged = true;
        invalidate(true);
        invalidateOutline();
      }
    view 에 변수 mBackground 가 있 는 것 을 볼 수 있 습 니 다.우리 가 설정 한 배경 은 Drawable 로 바 뀌 어 값 을 부여 합 니 다.그리고 어떻게 그 렸 는 지 볼 수 있 습 니 다.draw(그리 기 방법 을 찾 은 다음 배경 drawable(canvas)을 조작 하여 캔버스 를 전달 하 는 방법 이 있 습 니 다.이 방법 은 소스 코드 입 니 다.
    
    private void drawBackground(Canvas canvas) {
        final Drawable background = mBackground;
        if (background == null) {
          return;
        }
    
        setBackgroundBounds();
    
        // Attempt to use a display list if requested.
        if (canvas.isHardwareAccelerated() && mAttachInfo != null
            && mAttachInfo.mHardwareRenderer != null) {
          mBackgroundRenderNode = getDrawableRenderNode(background, mBackgroundRenderNode);
    
          final RenderNode renderNode = mBackgroundRenderNode;
          if (renderNode != null && renderNode.isValid()) {
            setBackgroundRenderNodeProperties(renderNode);
            ((DisplayListCanvas) canvas).drawRenderNode(renderNode);
            return;
          }
        }
    
        final int scrollX = mScrollX;
        final int scrollY = mScrollY;
        if ((scrollX | scrollY) == 0) {
          background.draw(canvas);
        } else {
          canvas.translate(scrollX, scrollY);
          background.draw(canvas);
          canvas.translate(-scrollX, -scrollY);
        }
      }
    그래서 자체 view 방법 을 이용 하여 배경 설정 을 간단하게 완성 하고 View 에 맡 기 면 됩 니 다.
    2.화판 을 그림 으로 저장
    그림 을 저장 하 는 데 는 세 가지 방법 이 있 습 니 다.
  • 자신 이 그린 비트 맵 을 스스로 저장 합 니 다
  • view 자체 의 bitmap
  • 를 이용 하여
  • view 를 이용 하여 bitmap 만 들 기
  • 2.1 자신 이 그린 비트 맵
    우리 의 이전 코드 는 bufferBitamp 과 bufferCanvas 를 이용 하여 그 렸 기 때문에 우리 의 내용 은 bufferBitmap 에 있 습 니 다.이것 을 그림 으로 저장 하면 됩 니 다.
    
     /**
       *     
       * @param path        
       */
      fun save(path: String){
    
        if(!TextUtils.isEmpty(path)){
    
          val f = File(path)
          if(f.exists()){
            f.delete()
          }
          try{
            val out = FileOutputStream(f)
            // 90             
            mBufferBitmap.compress(Bitmap.CompressFormat.JPEG,90,out)
    
            out.flush()
            out.close()
          }catch (e:Exception){
            e.printStackTrace()
          }
        }
      }
    그러나 이 방법 은 자체 draw 가 그린 배경 을 저장 할 수 없습니다.다음은 두 번 째 방법.
    2.2 View 의 drawing Cache
    View 에서 setDrawingCache Enabled 방법 이 있 습 니 다.이 방법 은 그림 cache 를 여 는 것 입 니 다.이 방법 은 속 도 를 증가 시 킬 수 있 지만 메모리 가 조금 차지 합 니 다.따라서 필요 하지 않 을 때 청 소 를 할 필요 가 있 습 니 다.destroy DrawingCache 나 setDrawingCache Enabled(false)를 통 해 이 루어 집 니 다.
    이 방법 을 열 면 getDrawingCache 방법 으로 현재 view 가 그린 bitmap 데 이 터 를 가 져 올 수 있 습 니 다.
    그래서 view 가 초기 화 될 때 캐 시 를 엽 니 다.
    
    init {
       mPaint.style = Paint.Style.STROKE //     
       mPaint.color = Color.RED     //  
       mPaint.strokeCap = Paint.Cap.ROUND //     
       mPaint.strokeWidth = 10f      //    
    
        //    
        isDrawingCacheEnabled = true
    
      }
    그림 을 저장 할 때 바로 가 져 오 면 됩 니 다.위의 mBufferBitmap 를 getCacheBitmap()로 바 꾸 면 됩 니 다.
    
    /**
     *   view      bitmap,
     *      setDrawingCacheEnabled(boolean enabled)
    */
      fun getCacheBitmap(): Bitmap{
    
        val bm = drawingCache
        val result = Bitmap.createBitmap(bm)
        //  build   
        destroyDrawingCache()
        return result
      }
    2.3 view 를 이용 하여 bitmap 만 들 기
    Bitmap 의 createBitmap 방법 을 이용 하여 현재 view 를 bitmap 으로 만 듭 니 다.
    
    fun getBitmap(v: View): Bitmap{
        val bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        v.draw(canvas)
        return bitmap
      }
    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기