zoukankan      html  css  js  c++  java
  • Android自定义可以在文字上方显示备注的View

    闲着没事干写写这个,算坐标要算晕了,恐怕半个月后自己都要看不懂了

    package com.example.texttest;
    
    
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.Rect;
    
    public class KString{
        private String[] mKString;
        private Paint mainPaint;
        private Paint subPaint;
        public int mainH,subH,mainW,subW,allW,allH;
        private Point mainStart,subStart;
        public KString(String main,String sub){
            mKString = new String[2];
            
            subPaint=new Paint();
            mainPaint=new Paint();
            subPaint.setAntiAlias(true);
            mainPaint.setAntiAlias(true);
            subPaint.setColor(Color.BLACK);
            mainPaint.setColor(Color.BLACK);
            mainPaint.setTextSize(20);
            subPaint.setTextSize(13);
            
            setText(main, sub);
            
            
        }
        
        public void setText(String main,String sub){
            mKString[0]=main;
            mKString[1]=sub;
            
            Rect mainR=new Rect();
            mainPaint.getTextBounds(mKString[0], 0, mKString[0].length(), mainR);
            Rect subR=new Rect();
            subPaint.getTextBounds(mKString[1], 0, mKString[1].length(), subR);
            mainH=mainR.bottom-mainR.top+1;
            mainW=mainR.right-mainR.left;
            subH=subR.bottom-subR.top+1;
            subW=subR.right-subR.left;
            mainStart=new Point(0, 0);
            subStart=new Point(0, 0);
            
            allW=Math.max(mainW, subW);
            allH=mainH+subH;
        }
        
        public void draw(Canvas c){
            c.drawText(mKString[0], mainStart.x, mainStart.y, mainPaint);
            c.drawText(mKString[1], subStart.x, subStart.y, subPaint);
                
        }
        
        public void setStartPoint(int x,int y,int mainH,int subH){
        
                y=y+(mainH+subH)-2;
                mainStart.x=x+(allW-mainW)/2;;
                mainStart.y=y;
                subStart.y=y-mainH;
                subStart.x=x+(allW-subW)/2;
                
            
        }
        
        public int getHeight(){
            return allH;
        }
        
        public int getWidth(){
            return allW;
        }
        
        
        
        
        
    
    
    }
    package com.example.texttest;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class MyText extends View {
        
        List<KString> mData;
        private int mainH=0,subH=0,charPadding=3,allW;
        
    
        public MyText(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            mData=new ArrayList<KString>();
            
            
        }
        
        public MyText(Context context){
            super(context);
        }
        
        public void setKString(String main,String sub){
            KString ks=new KString(main, sub);
            mData.add(ks);
            if(ks.mainH>mainH)
                mainH=ks.mainH;
            if(ks.subH>subH)
                subH=ks.subH;
        }
        
        public void setKString(String s){
            String[] sa=s.split(";");
            for(int i=0;i<sa.length;i+=2){
                setKString(sa[i], sa[i+1]);
            }
        }
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            allW=0;
            for(KString item:mData){
                item.setStartPoint(allW, 0, mainH, subH);
                allW+=item.allW+charPadding;
                item.draw(canvas);
            }
            
            
        }
        
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mainH+subH);
        }
        
        
        
        
    
    }
    package com.example.texttest;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            ((MyText)findViewById(R.id.myText1)).setKString("あんな; ;空;そら;で; ;ミサイル;misairu;が; ;飛;と;ぶのなら; ");
            ((MyText)findViewById(R.id.myText2)).setKString("そんな; ;物;もの;で; ;幸;しあわ;せを; ;乞;こ;うのなら; ");
            ((MyText)findViewById(R.id.myText3)).setKString("優;やさ;しい; ;人;ひと;にならなくちゃ; ");
            ((MyText)findViewById(R.id.myText4)).setKString("僕;ぼく;は; ;僕;ぼく;を; ;肯定;こうてい;していけるかな; ");
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <com.example.texttest.MyText
            android:id="@+id/myText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <com.example.texttest.MyText
            android:id="@+id/myText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/myText1"
            android:layout_marginTop="10dp" />
    
        <com.example.texttest.MyText
            android:id="@+id/myText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/myText2"
            android:layout_marginTop="10dp" />
    
        <com.example.texttest.MyText
            android:id="@+id/myText4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/myText3"
            android:layout_marginTop="10dp" />
    
    </RelativeLayout>
  • 相关阅读:
    meta标签总结
    基本类型String的原生方法详解
    对JSON的增删查改
    百分比宽度div如何水平居中
    【转】Chrome 控制台console的用法(提高js调试能力)
    css textarea固定大小滚动条自动
    【转】前端必读:浏览器内部工作原理
    git clone 远程分支
    http-server 使用介绍
    js 全选/取消
  • 原文地址:https://www.cnblogs.com/kagami/p/3016700.html
Copyright © 2011-2022 走看看