简体   繁体   中英

My Gradient colors is not the same on different screen sizes?

I don't understand why this happend, but I would like to know why and how to solve it. I want my gradient to look like in the Galaxy S7 picture for all devices or different screen sizes

在此处输入图片说明

The code for Gradient:

    textView = (TextView) v.findViewById(R.id.fontcolorwhite);

    LinearGradient linearGradient = new LinearGradient(60,-70,60,70, new int[]{Color.parseColor("#00a8d9"), Color.parseColor("#4244b8"), Color.parseColor("#f1328f"), Color.parseColor("#ee3031"), Color.parseColor("#fde92d"), Color.parseColor("#009e54")},null,
    Shader.TileMode.REPEAT);
    textView.getPaint().setShader(linearGradient);

Allright it now works perfect on all devices and screen size.

The end solution was to use dp instead of pixels and playing around with values till I got the right gradient:

    float px4 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
    float px5 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -17f, getResources().getDisplayMetrics());
    float px6 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
    float px7 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 18f, getResources().getDisplayMetrics());


    LinearGradient linearGradient = new LinearGradient(px4, px5, px6, px7, new int[]{Color.parseColor("#00a8d9"), Color.parseColor("#4244b8"), Color.parseColor("#f1328f"), Color.parseColor("#ee3031"), Color.parseColor("#fde92d"), Color.parseColor("#009e54")}, null,
            Shader.TileMode.REPEAT);
    fontcolorpickerbtn.getPaint().setShader(linearGradient);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM