自定义view 五角星 国旗
2016-12-29 13:34
281 查看
先看下效果
虽然有点丑,但效果还是有的。
步骤:
1.自定义五角星
2.排版
代码:
主界面
主界面布局
虽然有点丑,但效果还是有的。
步骤:
1.自定义五角星
2.排版
代码:
package com.example.yskj_jh.myapplication; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.view.View; /** * Created by YSKJ-JH on 2016/12/29. */ public class MyView extends View { private Context context; private Path path; public MyView(Context context) { super(context); this.context =context; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width=getWidth(); int r=width/2; float outR=getWidth()/2 /5; float inR=outR*sin(18)/sin(180-36-18); Paint paint=new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(5); paint.setColor(Color.RED); canvas.drawRect(30,30,700,500,paint); paint.setColor(Color.YELLOW); canvas.translate(r/5*2+15,200 ); canvas.rotate(-18); path = getCompletePath(outR, inR); paint.setStyle(Paint.Style.STROKE); canvas.drawPath(path, paint); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); canvas.translate(r/5*2,-50); canvas.rotate(-36); path = getCompletePath(outR/3, inR/3); paint.setStyle(Paint.Style.STROKE); canvas.drawPath(path, paint); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); canvas.translate(0,80); canvas.rotate(-18); path = getCompletePath(outR/3, inR/3); paint.setStyle(Paint.Style.STROKE); canvas.drawPath(path, paint); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); canvas.translate(-70,10); canvas.rotate(54); path = getCompletePath(outR/3, inR/3); paint.setStyle(Paint.Style.STROKE); canvas.drawPath(path, paint); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); canvas.translate(-70,50); canvas.rotate(-18); path = getCompletePath(outR/3, inR/3); paint.setStyle(Paint.Style.STROKE); canvas.drawPath(path, paint); paint.setStyle(Paint.Style.FILL); canvas.drawPath(path, paint); } private Path getCompletePath(float outR, float inR) { Path path=new Path(); path.moveTo(outR*cos(72*0), outR*sin(72*0)); path.moveTo(outR*cos(72*0), outR*sin(72*0)); path.lineTo(inR*cos(72*0+36), inR*sin(72*0+36)); path.lineTo(outR*cos(72*1), outR*sin(72*1)); path.lineTo(inR*cos(72*1+36), inR*sin(72*1+36)); path.lineTo(outR*cos(72*2), outR*sin(72*2)); path.lineTo(inR*cos(72*2+36), inR*sin(72*2+36)); path.lineTo(outR*cos(72*3), outR*sin(72*3)); path.lineTo(inR*cos(72*3+36), inR*sin(72*3+36)); path.lineTo(outR*cos(72*4), outR*sin(72*4)); path.lineTo(inR*cos(72*4+36), inR*sin(72*4+36)); path.close(); return path; } float cos(int num){ return (float) Math.cos(num*Math.PI/180); } float sin(int num){ return (float) Math.sin(num*Math.PI/180); } }
主界面
private FrameLayout frameLayout; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_draw_view); frameLayout = (FrameLayout) findViewById(R.id.fragment); frameLayout.addView(new MyView(DrawViewActivity.this)); }
主界面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout>
相关文章推荐
- Android中自定义一个View的方法详解
- Android App中自定义View视图的实例教程
- Android中自定义View实现圆环等待及相关的音量调节效果
- Android开发使用自定义view实现ListView下拉的视差特效功能
- 自定义图表控件--同时显示柱状图和折线图
- HTML5 canvas基本绘图之绘制五角星
- android自定义View的用法
- android自定义控件实例
- android 自定义控件的两种做法
- ImageView显示圆的特殊方法
- 自定义view的自定义属性的引用
- 自定义带圆点ViewPager
- 关于自定义View中的自定义属性
- 自定义固定宽高比例的Layout
- android 自定义View onMeasure
- Android中inflate和merge结合使用
- android在自定义View的xml中设置自定义的成员属性
- ListView自定义适配器 优化
- Android自定义View
- 自定义View -- 倒计时View