androidshape矩形
① 怎麼用Android畫一個正方形
先來介紹一下畫幾何圖形要用到的,畫布(Canvas)、畫筆(Paint)。
1. 畫一個圓使用的是drawCircle:canvas.drawCircle(cx, cy, radius, paint);x、y代表坐標、radius是半徑、paint是畫筆,就是畫圖的顏色;
2. 在畫圖的時候還要有注意,你所畫的矩形是實心(paint.setStyle(Paint.Style.FILL))還是空心(paint.setStyle(Paint.Style.STROKE);
畫圖的時候還有一點,那就是消除鋸齒:paint.setAntiAlias(true);
3. 還有就是設置一種漸變顏色的矩形:
Shader mShader = new LinearGradient(0,0,100,100, new int[]{Color.RED,Color.GREEn,Color.BLUE,Color.YELLO},null,Shader.TileMode.REPEAT);
ShapeDrawable sd;
//畫一個實心正方形
sd = new ShapeDrawable(new RectShape());
sd.setBounds(20,20,100,100);
sd.draw(canvas);
//一個漸變色的正方形就完成了
4. 正方形:drawRect:canvas.drawRect(left, top, right, bottom, paint)
這里的left、top、right、bottom的值是:
left:是矩形距離左邊的X軸
top:是矩形距離上邊的Y軸
right:是矩形距離右邊的X軸
bottom:是矩形距離下邊的Y軸
5. 長方形:他和正方形是一個原理,這個就不用說了
6. 橢圓形:記住,這里的Rectf是float類型的
RectF re = new Rect(left, top, right, bottom);
canvas.drawOval(re,paint);
好了,說了這么多的的東西,那就讓我們來看一下真正的實例吧!!!
package com.hades.game;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
public class CanvasActivity extends Activity {
/**
* 畫一個幾何圖形
* hades
* 藍色著衣
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView myView = new MyView(this);
setContentView(myView);
}
public class MyView extends View {
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 設置畫布的背景顏色
canvas.drawColor(Color.WHITE);
/**
* 定義矩形為空心
*/
// 定義畫筆1
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
// 消除鋸齒
paint.setAntiAlias(true);
// 設置畫筆的顏色
paint.setColor(Color.RED);
// 設置paint的外框寬度
paint.setStrokeWidth(2);
// 畫一個圓
canvas.drawCircle(40, 30, 20, paint);
// 畫一個正放形
canvas.drawRect(20, 70, 70, 120, paint);
// 畫一個長方形
canvas.drawRect(20, 170, 90, 130, paint);
// 畫一個橢圓
RectF re = new RectF(20, 230, 100, 190);
canvas.drawOval(re, paint);
/**
* 定義矩形為實心
*/
paint.setStyle(Paint.Style.FILL);
// 定義畫筆2
Paint paint2 = new Paint();
// 消除鋸齒
paint2.setAntiAlias(true);
// 設置畫筆的顏色
paint2.setColor(Color.BLUE);
// 畫一個空心圓
canvas.drawCircle(150, 30, 20, paint2);
// 畫一個正方形
canvas.drawRect(185, 70, 130, 120, paint2);
// 畫一個長方形
canvas.drawRect(200, 130, 130, 180, paint2);
// 畫一個橢圓形
RectF re2 = new RectF(200, 230, 130, 190);
canvas.drawOval(re2, paint2);
}
}
}
② android 怎麼畫一個圓角矩形
有兩種方法設置:
一:定義一個xml布局文件,分別設置四個角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 背景色 -->
<solid android:color="#FEBB66"/>
<!-- 邊框色 -->
<stroke android:width="0.3dip" android:color="#ffffff" />
<!-- 邊角:設置圓角大小 -->
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"/>
</shape>
二:把上面設置邊角的標簽屬性改成如下
<!-- 邊角:設置圓角大小 -->
<corners android:radius="5dp"/>
以上兩種效果是一樣的。然後引用這個布局文件就好
③ android怎樣在代碼中創建shape圓oval
在drawable文件夾中創建bg_oval_shape.xml的xml文件
文件中添加如下代碼
<?xmlversion="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#676767"/>
</shape>
3.在需要添加oval的控制項中引用,代碼如下:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_oval_shape"/>
④ android中如何使用shape來顯示直角三角形
用一個rectangle的shape,再用一個rotate標簽包裹這個shape,設置rotate標簽的fromDegree為30,pivotX為0%,pivotY為100%。
意思就是對矩形做一個旋轉,讓它變成直角三角形。

⑤ android 圓角樣式shape 的shape屬性什麼意思
shape官方給出了很多屬性的解釋,如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
><!-- 其中rectagle表示矩形,oval表示橢圓,line表示水平直線,ring表示環形 -->
<!-- 節點屬性介紹如下 -->
<corners />
<!-- 節點1:corners (圓角)
android:radius 圓角的半徑 值越大角越圓
android:topLeftRadius 左上圓角半徑
android:topRightRadius 右上圓角半徑
android:bottomLeftRadius 左下圓角半徑
android:bottomRightRadius 右下圓角半徑
-->
<gradient />
<!-- 節點2: gradient (背景顏色漸變)
android:startColor 起始顏色
android:centerColor 中間顏色
android:endColor 末尾顏色
android:angle 漸變角度,必須為45的整數倍。
android:type 漸變模式 默認是linear(線性漸變)radial(環形漸變)
android:centerX X坐標
android:centerY Y坐標
android:gradientRadius radial(環形漸變)時需指定半徑
-->
<padding />
<!-- 節點3: padding (定義內容離邊界的距離)
android:left 左部邊距
android:top 頂部邊距
android:right 右部邊距
android:bottom 底部邊距
-->
<size />
<!-- 節點4:size (大小)
android:width 指定寬度
android:height 指定高度
-->
<solid />
<!-- 節點5:solid (填充)
android:color 指定填充的顏色
-->
<stroke />
<!-- 節點6:stroke (描邊)
android:width 描邊的寬度
android:color 描邊的顏色
把描邊弄成虛線的形式"- - -":
android:dashWidth 表示'-'這樣一個橫線的寬度
android:dashGap 表示之間隔開的距離
-->
</shape></span>
⑥ android繪制圓角矩形為什麼圓角邊粗了
用shapedrawable畫一個圓角矩形作為按鈕的background,但是圓角的地方總是有點粗,和直接用xml做出來的背景不一樣
float[]
outerR = new float[] { 20f, 20f, 20f, 20f, 0, 0, 0, 0 };
Shape shape = new
RoundRectShape(outerR, null, null);
image.setBackgroundDrawable(new
CustomShapeDrawable(shape,0xffea0000));
class CustomShapeDrawable extends
ShapeDrawable {
int color ;
public CustomShapeDrawable(Shape s,int
color) {
super(s);
this.color =
color;
}
@Override
protected void onDraw(Shape shape, Canvas
canvas, Paint paint) {
paint.setStrokeJoin(Join.ROUND);
paint.setDither(true);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setColor(0xffea0000);
paint.setStrokeWidth(4f);
shape.draw(canvas, paint);
}
}
把paint.setStrokeJoin這行去掉再試試
⑦ android自定義shape 時xmlns怎麼自動
MainActivity如下:
package cn.testshape;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 自定義shape的使用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="250dip"
android:layout_height="50dip"
android:text="測試自定義shape的使用"
android:background="@drawable/background_selector"
android:textColor="@drawable/textcolor_selector"
android:layout_centerInParent="true"
android:gravity="center"
/>
</RelativeLayout>
background_selector.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_shape" android:state_pressed="true"/>
<item android:drawable="@drawable/default_shape"/>
</selector>
default_shape.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 定義矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定義邊框顏色 -->
<solid android:color="#d1d1d1" />
<!-- 定義圓角弧度 -->
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
/>
</shape>
pressed_shape.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 定義矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定義邊框顏色 -->
<solid android:color="#7bb3f8" />
<!-- 定義圓角弧度 -->
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
/>
</shape>
textcolor_selector.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#ffffff" android:state_pressed="true"/>
<item android:color="#000000"/>
</selector>
⑧ android用shape畫虛線,怎麼也不顯示
一直以為android的shape能畫直線,虛線,矩形,圓形等,畫直線也就算了,用一個view設一下高度和顏色,就可以出來一條直線了。所以說這個對我來說經常不用,圓形是可以,看看我應用里的消息提示框都是這樣生成的,好了,這個不存在問題,今天想要做是一條虛線,什麼也不說了,直接上虛線的代碼:<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<!-- 顯示一條虛線,破折線的寬度為dashWith,破折線之間的空隙的寬度為dashGap,當dashGap=0dp時,為實線 -->
<stroke android:width="1dp" android:color="#D5D5D5"
android:dashWidth="2dp" android:dashGap="3dp" />
<!-- 虛線的高度 -->
<size android:height="2dp" />
</shape>
解釋的好完美,真要不是不自己試,估計一輩子都會相信這是真的了,結果是放到手機里,從來沒有出現過什麼線條,對於像我一樣追求完美的人來說,自然不會放過這一個細節,在網上找了大半天,有的小夥伴們也遇到了,並且也解決了,可是把他們的方法拿過來後,都一個也不好使,實際情況是還是不能顯示,總結一下小夥伴們的解決方法吧
1.從android3.0開始,安卓關閉了硬體加速功能,所以就不能顯示了,所以就是在 AndroidManifest.xml,或者是在activity中把硬體加速的功能關掉就可以了android:hardwareAccelerated="false"或者是view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
2.一個小夥伴的翻譯,說什麼height要大於dashWidth才能顯示。
我都按他們的方法試了,根本都不行,
所以要想真正的實現還是老老實實的自己去畫一條虛線吧。
⑨ android shape 怎麼 畫菱形背景
可以使用多段線拾取矩形或者正方形四邊中點繪制,也可以通過多段線繪制角度線方式完成,還可以通過拖拽矩形或正方形頂點變形完成。邊長可以用dal命令測量尺寸
⑩ 安卓中shape矩形框能自適應大小嗎
可以啊,按鈕的話也可以做成一個小圖,然後控制圖片img用百分比來設置,樣式如果不美觀的話可以用border來設置一下,這樣的話移動端的按鈕也不會因為瀏覽器大小而變動;
