Java – Android image view gradient background

Therefore, I don't want to update ImageView to change the gradient according to some events of mobile phone gyroscope and lightsensor But now I'm trying to click on events

I want to set a radial gradient with a center in the click event I first consider setting the layout background. Everything is fine Then I try to change it for ImageView (the desired visual element in size and position) in the layout, and the gradient center moves down to the right

This is the code:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seeker);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.seeker_layout);

    layout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v,MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                int location[] = new int[2];
                //measuring location of Image View to try and find correlation to shift
                findViewById(R.id.gradient_indicator).getLocationOnScreen(location);
                int grad_height = findViewById(R.id.gradient_indicator).getMeasuredHeight();
                int grad_width = findViewById(R.id.gradient_indicator).getMeasuredWidth();
                final float pos_x = event.getX();
                final float pos_y = event.getY();
                Toast position = Toast.makeText(getApplicationContext(),"Event Position (" + String.valueOf(pos_x) + "x" + String.valueOf(pos_y) + ")\n Window Dimensions(" + grad_width + "x" + grad_height + ")\n Window Position(" + location[0] + "x" + location[1] + ")",Toast.LENGTH_LONG);
                position.show();

                ShapeDrawable.ShaderFactory shader_factory = new ShapeDrawable.ShaderFactory() {

                    @Override
                    public Shader resize(int width,int height) {
                        RadialGradient radialGradient = new RadialGradient(pos_x,pos_y,350,0xff0000ff,Shader.TileMode.MIRROR);
                        return radialGradient;
                    }
                };

                //Set curve radius to equal values in dp as specified in loaded xml shape
                float dp_radius = 5;
                int curve = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp_radius,getResources().getDisplayMetrics());
                float[] r_radii = new float[8];
                Arrays.fill(r_radii,curve);

                //create shape programatically that matches xml one
                RoundRectShape rs = new RoundRectShape(r_radii,null,null);
                ShapeDrawable sd = new ShapeDrawable(rs);
                sd.setShaderFactory(shader_factory);

                ImageView gradient_indicator = (ImageView) findViewById(R.id.gradient_indicator);

                if (Build.VERSION.SDK_INT >= 15) {
                    setBackgroundV15Plus(gradient_indicator,sd);
                } else {
                    setBackgroundV15Minus(gradient_indicator,sd);
                }


            }
            return true;
        }
    });
}
@TargetApi(15)
private void setBackgroundV15Plus(View view,ShapeDrawable sd) {
    view.setBackground(sd);

}

@SuppressWarnings("deprecation")
private void setBackgroundV15Minus(View view,ShapeDrawable sd) {
    view.setBackgroundDrawable(sd);
}

This is the image of the result I got. I marked the position of the cursor with a red circle Displaced Gradient

Solution

Like the working solution suggested by nanoc (it's not super elegant, but how it works): my solution is to add margins to the XML ImageView declaration You can then access margins from Java without converting DP pixel units Subtract the event location and 'all Code to get margin: ImageView gradient_ indicator =(ImageView)findViewById(R.id.gradient_indicator); RelativeLayout. LayoutParams lp =(RelativeLayout.LayoutParams)gradient_ indicator. getLayoutParams(); int grad_ margin_ top = lp. topMargin; int grad_ margin_ left = lp. leftMargin;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>