• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > android 自定义View圆圈箭头_

android 自定义View圆圈箭头_

作者:FH_94 字体:[增加 减小] 来源:互联网 时间:2017-11-18

FH_94通过本文主要向大家介绍了android 自定义View等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

创建自定义View类:

 

public class MyCircleView extends View{

    //当前画笔画圆的颜色
    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();
        //旋转画布 , 如果旋转的的度数大的话,视觉上看着是旋转快的
        canvas.rotate(currentDegree,pivotX,pivotY);
        //提供了一些api可以用来画线(画路径)
        Path path = new Path();
        //从哪开始画 从A开始画
        path.moveTo(pivotX+radius,pivotY);
        //从A点画一个直线到D点
        path.lineTo(pivotX+radius-20,pivotY-20);
        //从D点画一个直线到B点
        path.lineTo(pivotX+radius,pivotY+20);
        //从B点画一个直线到C点
        path.lineTo(pivotX+radius+20,pivotY-20);
        //闭合  --  从C点画一个直线到A点
        path.close();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        canvas.drawPath(path,paint);
        canvas.restore();
        //旋转的度数一个一个度数增加,  如果乘以一个速度的话,按一个速度速度增加
        currentDegree+=1*currentSpeed;
        if (!isPause){
            invalidate();
        }
    }
    public void speed(){
        ++currentSpeed;
        if (currentSpeed>=10){
            currentSpeed=10;
            Intent intent = new Intent(getContext(), MovieActivity.class);
            getContext().startActivity(intent);
            Toast.makeText(getContext(),"我比闪电还快", Toast.LENGTH_SHORT).show();
        }
    }
    public void slowDown(){
        --currentSpeed;
        if (currentSpeed<=1){
            currentSpeed=1;
        }
    }
    public void pauseOrStart(){
        //如果是开始状态的话去重新绘制
        if (isPause){
            isPause=!isPause;
            invalidate();
        }else {
            isPause=!isPause;
        }
    }
}

 

 

MainActivity调用:

 

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();
    }
}


布局:

 

 

 <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="设置颜色" />

    <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="暂定/开始" />
    <code.view.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"
        />

 

 

attrs文件:

 

<resources>
    <declare-styleable name="MyCircleView">
        <attr name="circlr_bound_width" format="dimension"></attr>
        <attr name="circlr_bound_color" format="color"></attr>
    </declare-styleable>
</resources>

 

 

 

 

 

 

 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-26MaterialRefreshLayout,swiperefreshlayout
  • 2017-05-222.4.9 ListView的数据更新问题
  • 2017-05-26Android属性动画
  • 2017-05-26Android开发7:简单的数据存储(使用SharedPreferences)和文件操作,
  • 2017-10-21在Mac系统中 下载、安装AndroidStudio
  • 2017-11-18android 权限拒绝时的问题解决(详解)
  • 2017-05-26谷歌电子市场1--BaseFragment,1--basefragment
  • 2017-05-26LVS 内核实现分析(1)
  • 2017-05-26Android--实现ViewPager边界回弹效果(转),android--viewpager
  • 2017-05-26Android Studio快捷键指南(本文持续更新)

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 4.5.1 Intent的基本使用
    • Intent(三)向下一个活动传递数据,intent传递
    • androidStudio通过svn进行版本控制,androidstudiosvn
    • Android数据存储之SQLite
    • Charles Proxy 4.1.2 破解版,charles4.1.2
    • 在Android Studio中安装OpenCV mac环境/Linux环境
    • 8.3.7 Paint API之—— Xfermode与PorterDuff详解(四)
    • Android学习笔记-ImageView(图像视图),android-imageview
    • 解决webview调用 goBack() 返回上一页自动刷新闪白的情况,webviewgoback
    • 学习《第一行代码》使用实机测试所遇问题(一),《第一行代码》

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有