G_ibsonl 2016-03-11 06:44 采纳率: 0%
浏览 3424

Android自定义View画笔颜色改变问题(画笔颜色改变了,但是实际上ABCD这些字母的颜色没变)

package com.eage.tbw.view;

import com.eage.tbw.R;
import com.eage.tbw.R.color;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**

  • A-Z字母的View
  • lxm
    */
    public class LetterView extends View {
    private static final String[] LETTER=new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"};

    private Paint paint;
    private int textSize;
    private int choice;

    private TextView outerTV;

    private LetterOnTouchCallBack letterOnTouchCallBack;

    public void setTextView(TextView textView){
    outerTV=textView;

    }

    public LetterView(Context context) {
    this(context, null);
    }

    public LetterView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);

    }

    public LetterView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray typedArray=context.getTheme().obtainStyledAttributes(attrs, R.styleable.LetterView, defStyleAttr, 0);

    textSize=typedArray.getInt(R.styleable.LetterView_text, 27);
    
    typedArray.recycle();
    initView();
    

    }

    private void initView() {
    paint=new Paint();

    paint.setTextSize(textSize);
    paint.setColor(color.custom_yellow);
    paint.setAntiAlias(true);
    
    choice=-1;
    

    }

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int height=getHeight();
    int width=getWidth();
    
    int length=LETTER.length;
    int itemHeigth=height/length;
    
    for(int i=0;i<length;i++){
        if(i==choice-1){
            paint.setColor(color.custom_yellow);
            paint.setTextSize(32);
        }else{
            paint.setColor(color.custom_yellow);
            paint.setTextSize(textSize);
    
        }
    
        float y=(i+1)*itemHeigth; 
        float x=(width-paint.measureText(LETTER[i]))/2;
        canvas.drawText(LETTER[i], x, y, paint);
    }
    

    }

    //重写触摸事件
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

    final LetterOnTouchCallBack callBack = letterOnTouchCallBack;
    
    float  y=event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_UP:
        choice=-1;
        if(outerTV!=null){
            outerTV.setVisibility(View.GONE);
        }
        setBackgroundColor(Color.WHITE);
        invalidate();
        break;
    
    default:
        int length=LETTER.length;
        int position= (int) (y/getHeight()*LETTER.length);
        setBackgroundColor(Color.parseColor("#E5E5E5"));
        if(1<=position&&position<=length){
            //将选中的字母传回到MainActivity中
            if(callBack!=null){
                callBack.onTouchingLetterChanged(LETTER[position-1]);
            }
            if(outerTV!=null){
                outerTV.setText(LETTER[position-1]);
                outerTV.setVisibility(View.VISIBLE);
            }
            choice=position;
            invalidate();
        }
        break;
    }
    
    return true;
    

    }

    // 设置监听

    public void setOnLetterCallBack(LetterOnTouchCallBack letterOnTouchCallBack) {
        this.letterOnTouchCallBack = letterOnTouchCallBack;
    }
    
    //接口
    public interface LetterOnTouchCallBack {
    
        public void onTouchingLetterChanged(String s);
    
    }
    

}

  • 写回答

5条回答 默认 最新

  • G_ibsonl 2016-03-11 06:47
    关注

    图片说明就是不变颜色啊,求大神告诉下问题

    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?