Android学习笔记(二)自定义View(1)

今天播放器编写没有太大的进展主要是实现了歌词在自定义View上的显示。我参考了一些大神写的自定义View的方法,加上自己的理解磕磕绊绊算是写完了,不过在将文本画到View中是遇到了一些问题,主要是Canvas的drawText()

的方法中X和Y的实际表示的位置,通过半天的验证算是搞明白了,X表示的是一个相对的坐标,它是相对于自定义View左边界的大小,同理Y是相对于自定义View上边界的大小。弄明白这个问题之后进行自定义View的就简单了很多。具体的实例如下(菜鸟写法,有很多不规范的地方还请各位提出来,相互促进相互交流):

public class LyricListView extends View {
    //将歌词文件保存在到歌词集合<span style="white-space:pre">	</span>
    private List<Lyric> lyricList;
    private int currIndex = 0;
    private float startY;
    private float nowY;
    private float offSet;
    private float offsetY;
   
    private  Paint paint;
<pre name="code" class="html">    //自定义view在实际布局中的宽度
private int width;

 
<pre name="code" class="html">    //自定义view在实际布局中的高度
private int height; public LyricListView(Context context) { super(context); } public LyricListView(Context context, AttributeSet attrs) { super(context, attrs); //a = (Activity) context; this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); this.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: //手指按到屏幕上 startY = event.getY(); break; case MotionEvent.ACTION_MOVE: nowY = event.getY(); offSet = offsetY+nowY-startY; postInvalidate(); break; case MotionEvent.ACTION_UP: Log.i("YI",nowY-startY+"Y-Y"); offSet = offsetY+ nowY-startY; offsetY =offSet; startY = 0; nowY = 0; postInvalidate(); } return true; } }); } public void changeData(List<Lyric> list,int index,int height) { //height 为LyricListView 在实际布局中的高度。 this.height = height; currIndex = index; offSet = currIndex * -150; offsetY =offSet; lyricList = list; super.postInvalidate(); } public LyricListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Log.i("NUO","Height:"+Global.SCREEN_HEIGHT+" "); paint = new Paint(); if(lyricList != null) { qukong(lyricList); for(int i=0;i<lyricList.size();i++) { if (i == currIndex) { paint.setColor(Color.WHITE); //高亮 设置为20sp TypedValue..applyDimension()进行sp dp px之间的转换 float highLight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,getResources().getDisplayMetrics()); paint.setTextSize(highLight); } else { //普通设置为14sp float normal = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,14,getResources().getDisplayMetrics()); paint.setColor(Color.GRAY); paint.setTextSize(normal); } String lrc = lyricList.get(i).getLrc(); float textWidth = paint.measureText(lrc); Paint.FontMetrics fm = paint.getFontMetrics();//获取字体的度量(尺寸) float textHeight = fm.descent - fm.ascent; if (textWidth < height * 9 / 10) { drawText(lrc,textWidth, textHeight,i,canvas); } else { String firLrc = lrc.substring(0,lrc.length()/2); String secLrc = lrc.substring(lrc.length()/2); drawText(firLrc,paint.measureText(firLrc),textHeight,i,canvas); offSet +=150; drawText(secLrc,paint.measureText(secLrc),textHeight,i,canvas); } } } } private void drawText(String txt,float txtWidth,float txtHeight,int index,Canvas canvas ) { //经试验得到canvas中的x和y均是相对坐标,相对于自己外边界的。不是屏幕上的绝对坐标。 canvas.drawText(txt, (Global.SCREEN_WIDTH - txtWidth) / 2, (height - txtHeight) / 2 + index* 150 + 75 + offSet, paint); if(index == currIndex) { Log.i("NUO","curr"+currIndex+txt+" height:"+ ((height - txtHeight) / 2 + index* 150 + 75) +" offset:"+ "txtHeight"+offSet); } } }
 
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值