五星好评星星实现

/**
 * Created by wanggang
 * on 2018/7/12.
 */
public class GradeUI extends Activity {

    private Context mContext = null;
    private StarIconView ratingBar;//星级评分条
    private float score = 0;
    private final static int REQUESTCODE = 1; // 返回的结果码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        try {
            initView(this);
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }
    }


    public void initView(final Context context) {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        // 设置背景颜色
        Resources resources = context.getResources();
        Drawable drawable = resources.getDrawable(R.drawable.self_translate);
        getWindow().setBackgroundDrawable(drawable);
//        String name = GradeBiz.getInstance().getAppName(this);


        RelativeLayout layout = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(layoutParams);
        RelativeLayout ivLayout = new RelativeLayout(mContext);
        try {
            ivLayout = new RelativeLayout(mContext);
            RelativeLayout.LayoutParams ivLayoutParams = new RelativeLayout.LayoutParams(width * 250 / 300, height * 212 / 600);
            ivLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            ivLayout.setBackgroundColor(Color.WHITE);
            ivLayout.setLayoutParams(ivLayoutParams);


            RelativeLayout iconLayout = new RelativeLayout(this);
            RelativeLayout.LayoutParams iconLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            iconLayout.setLayoutParams(iconLayoutParams);



            TextView text1 = new TextView(this);
            text1.setText("Enjoy the app?");
            text1.setTextColor(Color.BLACK);
            text1.setTextSize(25);
//            TextPaint paint = text1.getPaint();
//            paint.setFakeBoldText(true);
            RelativeLayout.LayoutParams ivText1Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            ivText1Params.setMargins(0, width / 30, 0, 0);
            ivText1Params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            text1.setLayoutParams(ivText1Params);
            iconLayout.addView(text1);
//            iconLayout.addView(icon);



            RelativeLayout layout1 = new RelativeLayout(this);
            RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//            LayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            LayoutParams.setMargins(0, (int)(height*0.12), 0, 0);
            layout1.setLayoutParams(LayoutParams);


            ratingBar = new StarIconView(this);
            ratingBar.setPointIntegerMark(true);
            ratingBar.setOnStarChangeListener(new StarIconView.OnPointStarChangeListener() {
                public void onPointStarChange(float mark) {
                    Logger.d("分数为:" + mark);
                }
            });
            RelativeLayout.LayoutParams ratingBarParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            ratingBarParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            ratingBar.setLayoutParams(ratingBarParams);
            layout1.addView(ratingBar);

            TextView text2 = new TextView(this);
            text2.setTextSize(12);
            text2.setText("Would you mind rating us?");
            text2.setTextColor(Color.BLACK);
            RelativeLayout.LayoutParams ivText2Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            ivText2Params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            ivText2Params.setMargins(0, (int)(height*0.22), 0, 0);
            text2.setLayoutParams(ivText2Params);

            TextView advice = new TextView(this);
            advice.setTextSize(18);
            advice.setTextColor(Color.BLACK);
            advice.setText("Later");
            RelativeLayout.LayoutParams cancelParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            cancelParams.setMargins(width / 20, 0, 0, height / 30);
            cancelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            cancelParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            advice.setLayoutParams(cancelParams);
            advice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });

            TextView sub = new TextView(this);
            sub.setText("Rate Now");
            sub.setTextSize(18);
            sub.setTextColor(0xff009688);
            RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            btnParams.setMargins(0, 0, width / 20, height / 30);
            btnParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            btnParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            sub.setLayoutParams(btnParams);
            sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    /**
                     * getRating():用于获取等级,表示选中的几颗星
                     * getStepSize():用语获取每次至少要改变多少个星级
                     * getProgress():用语获取进度,获取到的进度值为getRating()方法返回值与getStepSize()方法返回值之积
                     */
                    /**
                     * getRating():用于获取等级,表示选中的几颗星
                     * getStepSize():用语获取每次至少要改变多少个星级
                     * getProgress():用语获取进度,获取到的进度值为getRating()方法返回值与getStepSize()方法返回值之积
                     */
                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.setPackage("com.android.vending");
                    mContext.startActivity(intent);
                    SharedPreferences spf = context.getSharedPreferences("type_Evaluate", Context.MODE_PRIVATE);
                    spf.edit().putBoolean("isSubmit", true).commit();
                    finish();
                }
            });


            ivLayout.addView(layout1);
            ivLayout.addView(sub);
            ivLayout.addView(advice);

            ivLayout.addView(text2);

            ivLayout.addView(iconLayout);
        } catch (Exception e) {
            Logger.printStackTrace(e);
        } catch (Error e) {
        }
        layout.addView(ivLayout);
        setContentView(layout);
    }
}
public class StarIconView extends View {

    private int pointStarDistance = 0; //星星间距
    private int pointStarCount = 5;  //星星个数
    private int pointStarSize;     //星星高度大小,星星一般正方形,宽度等于高度
    private Bitmap pointStarFillBitmap; //亮星星
    private Drawable pointStarEmptyDrawable; //暗星星
    private Paint pointPaint;         //绘制星星画笔


    private boolean pointIntegerMark = false;
    private OnPointStarChangeListener onStarChangeListener;//监听星星变化接口
    private float pointStarMark = 0.0F;   //评分星星

    public StarIconView(Context context) {
        super(context);
        init(context);
    }

    public StarIconView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化UI组件
     *
     * @param context 当前视图
     */
    private void init(Context context){
        setClickable(true);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;


        if(width 1) {
            canvas.drawRect(0, 0, pointStarSize, pointStarSize, pointPaint);
            if(pointStarMark -(int)(pointStarMark) == 0) {
                for (int i = 1; i < pointStarMark; i++) {
                    canvas.translate(pointStarDistance + pointStarSize, 0);
                    canvas.drawRect(0, 0, pointStarSize, pointStarSize, pointPaint);
                }
            }else {
                for (int i = 1; i < pointStarMark - 1; i++) {
                    canvas.translate(pointStarDistance + pointStarSize, 0);
                    canvas.drawRect(0, 0, pointStarSize, pointStarSize, pointPaint);
                }
                canvas.translate(pointStarDistance + pointStarSize, 0);
                canvas.drawRect(0, 0, pointStarSize * (Math.round((pointStarMark - (int) (pointStarMark))*10)*1.0f/10), pointStarSize, pointPaint);
            }
        }else {
            canvas.drawRect(0, 0, pointStarSize * pointStarMark, pointStarSize, pointPaint);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        if (x < 0) x = 0;
        if (x > getMeasuredWidth()) x = getMeasuredWidth();
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN: {
                setPointStarMark(x*1.0f / (getMeasuredWidth()*1.0f/ pointStarCount));
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                setPointStarMark(x*1.0f / (getMeasuredWidth()*1.0f/ pointStarCount));
                break;
            }
            case MotionEvent.ACTION_UP: {
                break;
            }
        }
        invalidate();
        return super.onTouchEvent(event);
    }

    /**
     * drawable转bitmap
     *
     * @param drawable
     * @return bitmap
     */
    private Bitmap drawableToBitmap(Drawable drawable)
    {
        if (drawable == null)return null;
        Bitmap bitmap = Bitmap.createBitmap(pointStarSize, pointStarSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, pointStarSize, pointStarSize);
        drawable.draw(canvas);
        return bitmap;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(android)