這篇文章主要為大家詳細(xì)介紹了怎么在Android中通過自定義View實(shí)現(xiàn)一個(gè)箭頭沿圓轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,發(fā)現(xiàn)的小伙伴們可以參考一下:

創(chuàng)新互聯(lián)擁有十年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù),對(duì)于網(wǎng)頁設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、app開發(fā)定制、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、主機(jī)域名等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
具體代碼如下所示:
//MyCircleView類
public class MyCircleView extends View{
//當(dāng)前畫筆畫圓的顏色
private int CurrenCircleBoundColor;
private Paint paint;
////從xml中獲取的顏色
private int circleBundColor;
private float circleBoundWidth;
private float pivotX;
private float pivotY;
private float radius=130;
private float currentDegree=0;
private int currentSpeed=1;
private boolean isPause=false;
public MyCircleView(Context context) {
super(context);
initView(context);
}
public MyCircleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCircleView);
for (int i = 0; i < typedArray.getIndexCount(); i++) {
//就是我們自定義的屬性的資源id
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.MyCircleView_circlr_bound_color:
circleBundColor = typedArray.getColor(attr, Color.RED);
CurrenCircleBoundColor=circleBundColor;
break;
case R.styleable.MyCircleView_circlr_bound_width:
circleBoundWidth = typedArray.getDimension(attr, 3);
break;
}
}
}
public MyCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context){
paint = new Paint();
}
public void setColor(int color){
if (CurrenCircleBoundColor!=color){
CurrenCircleBoundColor=color;
}else {
CurrenCircleBoundColor=circleBundColor;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(CurrenCircleBoundColor);
paint.setStrokeWidth(circleBoundWidth);
paint.setStyle(Paint.Style.STROKE);
pivotX = getWidth() / 2;
pivotY = getHeight() / 2;
canvas.drawCircle(pivotX,pivotY,radius,paint);
canvas.save();
//旋轉(zhuǎn)畫布 , 如果旋轉(zhuǎn)的的度數(shù)大的話,視覺上看著是旋轉(zhuǎn)快的
canvas.rotate(currentDegree,pivotX,pivotY);
//提供了一些api可以用來畫線(畫路徑)
Path path = new Path();
//從哪開始畫 從A開始畫
path.moveTo(pivotX+radius,pivotY);
//從A點(diǎn)畫一個(gè)直線到D點(diǎn)
path.lineTo(pivotX+radius-20,pivotY-20);
//從D點(diǎn)畫一個(gè)直線到B點(diǎn)
path.lineTo(pivotX+radius,pivotY+20);
//從B點(diǎn)畫一個(gè)直線到C點(diǎn)
path.lineTo(pivotX+radius+20,pivotY-20);
//閉合 -- 從C點(diǎn)畫一個(gè)直線到A點(diǎn)
path.close();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
canvas.drawPath(path,paint);
canvas.restore();
//旋轉(zhuǎn)的度數(shù)一個(gè)一個(gè)度數(shù)增加, 如果乘以一個(gè)速度的話,按一個(gè)速度速度增加
currentDegree+=1*currentSpeed;
if (!isPause){
invalidate();
}
}
public void speed(){
++currentSpeed;
if (currentSpeed>=10){
currentSpeed=10;
Toast.makeText(getContext(),"我比閃電還快",Toast.LENGTH_SHORT).show();
}
}
public void slowDown(){
--currentSpeed;
if (currentSpeed<=1){
currentSpeed=1;
}
}
public void pauseOrStart(){
//如果是開始狀態(tài)的話去重新繪制
if (isPause){
isPause=!isPause;
invalidate();
}else {
isPause=!isPause;
}
}
}
//主頁面
public class MainActivity extends AppCompatActivity {
//全局變量
private MyCircleView my_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
my_view = (MyCircleView) findViewById(R.id.my_view);
}
public void onClick(View view){
my_view.setColor(Color.BLUE);
}
public void add(View view){
my_view.speed();
}
public void slow(View view){
my_view.slowDown();
}
public void pauseOrStart(View view){
my_view.pauseOrStart();
}
}
主頁面布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lx_20170928.MainActivity">
<Button
android:id="@+id/set_color_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="onClick"
android:text="設(shè)置顏色" />
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/set_color_btn"
android:layout_centerHorizontal="true"
android:onClick="add"
android:text="加速" />
<Button
android:id="@+id/slow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
android:layout_centerHorizontal="true"
android:onClick="slow"
android:text="減速" />
<Button
android:id="@+id/pause_or_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/slow"
android:layout_centerHorizontal="true"
android:onClick="pauseOrStart"
android:text="暫定/開始" />
<com.example.lx_20170928.MyCircleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_view"
android:layout_centerInParent="true"
app:circlr_bound_color="@color/colorAccent"
app:circlr_bound_width="3dp"
/>
</RelativeLayout>
//在values建一個(gè)attrs.xml
<resources>
<declare-styleable name="MyCustomCircleArrowView">
<attr name="circlr_bound_width" format="dimension"></attr>
<attr name="circlr_bound_color" format="color"></attr>
</declare-styleable>
</resources>以上就是創(chuàng)新互聯(lián)小編為大家收集整理的怎么在Android中通過自定義View實(shí)現(xiàn)一個(gè)箭頭沿圓轉(zhuǎn)動(dòng)效果,如何覺得創(chuàng)新互聯(lián)網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將創(chuàng)新互聯(lián)網(wǎng)站推薦給身邊好友。
網(wǎng)頁標(biāo)題:怎么在Android中通過自定義View實(shí)現(xiàn)一個(gè)箭頭沿圓轉(zhuǎn)動(dòng)效果
當(dāng)前地址:http://chinadenli.net/article0/jijooo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、自適應(yīng)網(wǎng)站、定制網(wǎng)站、虛擬主機(jī)、搜索引擎優(yōu)化、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)