Android Shape 形狀

來源:互聯網
上載者:User

標籤:android   shape   


Shape繼承體系:

Shape (android.graphics.drawable.shapes)
----PathShape (android.graphics.drawable.shapes)
----RectShape (android.graphics.drawable.shapes)
--------ArcShape (android.graphics.drawable.shapes)
--------OvalShape (android.graphics.drawable.shapes)
--------RoundRectShape (android.graphics.drawable.shapes)


RectShape

RectShape rectShape = new RectShape();ShapeDrawable drawable = new ShapeDrawable(rectShape);drawable.getPaint().setColor(Color.RED);drawable.getPaint().setStyle(Paint.Style.FILL); //填充view.setBackgroundDrawable(drawable);

 矩形


RoundRectShape

float[] outerRadii = {20, 20, 40, 40, 60, 60, 80, 80};//外矩形 左上、右上、右下、左下 圓角半徑//float[] outerRadii = {20, 20, 20, 20, 20, 20, 20, 20};//外矩形 左上、右上、右下、左下 圓角半徑RectF inset = new RectF(100, 100, 200, 200);//內矩形距外矩形,左上方x,y距離, 右下角x,y距離float[] innerRadii = {20, 20, 20, 20, 20, 20, 20, 20};//內矩形 圓角半徑//RoundRectShape roundRectShape = new RoundRectShape(outerRadii, inset, innerRadii);RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null, innerRadii); //無內矩形ShapeDrawable drawable = new ShapeDrawable(roundRectShape);drawable.getPaint().setColor(Color.MAGENTA);drawable.getPaint().setAntiAlias(true);drawable.getPaint().setStyle(Paint.Style.STROKE);//描邊view.setBackground(drawable);

 無內矩形的圓角矩形  帶內矩形的圓角矩形


OvalShape

OvalShape ovalShape = new OvalShape();ShapeDrawable drawable = new ShapeDrawable(ovalShape);drawable.getPaint().setColor(Color.RED);drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);view.setBackgroundDrawable(drawable);

 橢圓。 當View的寬高相等時,就繪出了圓


ArcShape

ArcShape arcShape = new ArcShape(45, 270); //順時針  開始角度45, 掃描的角度270   扇形ShapeDrawable drawable = new ShapeDrawable(arcShape);drawable.getPaint().setColor(Color.RED);drawable.getPaint().setStyle(Paint.Style.FILL);// Bitmap bitmap =  ((BitmapDrawable)getResources().getDrawable(R.drawable.aa)).getBitmap();// BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader//                .TileMode.REPEAT);// Matrix matrix = new Matrix();// matrix.preScale(600.00f / bitmap.getWidth(), 600.00f / bitmap.getHeight());//view:w=600,h=600// bitmapShader.setLocalMatrix(matrix);// drawable.getPaint().setShader(bitmapShader);view.setBackgroundDrawable(drawable);

 扇形圖

 結合BitmapShader


PathShape

Path path = new Path();path.moveTo(50, 0);path.lineTo(0, 50);path.lineTo(50, 100);path.lineTo(100, 50);path.lineTo(50, 0);PathShape pathShape = new PathShape(path, 200, 100);ShapeDrawable drawable = new ShapeDrawable(pathShape);drawable.getPaint().setColor(Color.RED);drawable.getPaint().setStyle(Paint.Style.FILL);imageView.setBackgroundDrawable(drawable);

以Path路徑對象,來設定圖形。

PathShape的建構函式:PathShape(path, stdWidth, stdHeight);

   stdWidth:標準寬度

   stdHeight:標準高度

 在構造PathShape對象時,設定了寬高的標準。內建函式 

protected void onResize(float width, float height) {    mScaleX = width / mStdWidth;    mScaleY = height / mStdHeight;}public void draw(Canvas canvas, Paint paint) {    canvas.save();    canvas.scale(mScaleX, mScaleY);    canvas.drawPath(mPath, paint);    canvas.restore();}
  Shape基類中有函數 resize(),其中調用了onResize();ShapeDrawable中會調用resize()。

  有了設定的標準寬高,再算出實際寬高與標準寬高的比率,最後在繪製時,畫布canvas縮放。

  造成的效果: path中的(x,y)座標值 乘以 比率值,即是 最終呈現出的座標值(實際內部是縮放的canvas)

  比如,這裡view的 w=400, h=400

  如果標準寬高都等於400,那麼canvas最終不縮放,即1:1。 

  PathShape pathShape = new PathShape(path, 400, 400);

    stdx=400, stdy=400


PathShape pathShape = new PathShape(path, 100, 100);

 stdx=100, stdy=100  


PathShape pathShape = new PathShape(path, 200, 100);

 stdx=200, stdy=100


  



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android Shape 形狀

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.