Search code examples
androidcanvasdrawpaint

Can we create a canvas programmatically from a view in Android


I am trying to create a dummy view from already existing View.

Original Image:

From this image

Need to create dummy view like this.

to this image

I tried with paint and canvas.

public class MyView extends View {

    Paint paint;
    Path path;

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

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        canvas.drawRect(30, 50, 200, 350, paint);
//        canvas.drawRect(100, 100, 300, 400, paint);
        //drawRect(left, top, right, bottom, paint)

    }

}

But I cannot draw like this. Because some time image will be circle or Ovel or any shape. So, I need to deduct the existing view and draw new view as same. Can anyone help me to create a dummy view from existing view?

I am trying to do this for shimmer animation only. For facebook shimmer I need to give the view inside the shimmerFramelayout. But My view will be dynamic. So, I need to create a dummy view programmatically for every time. For facebook Shimmer:

<com.facebook.shimmer.ShimmerFrameLayout
        android:id="@+id/shimmerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--add  several shimmer placeholder layout -->

            <include layout="@layout/shimmer_placeholder_layout"></include>

            <include layout="@layout/shimmer_placeholder_layout"></include>

            <include layout="@layout/shimmer_placeholder_layout"></include>
        </LinearLayout>
    </com.facebook.shimmer.ShimmerFrameLayout>

Here shimmer_placeholder_layout is static view. I need to create dynamic view.


Solution

  • Finally got the solution.

    Here I attached the solution for someone will get benefit on this. MyView class:

    public class MyView extends View {
    
        Paint paint;
        ViewGroup viewGroup;
        Context context;
        List<Rect> rectList = new ArrayList<>();
    
        public MyView(Context context, ViewGroup viewGroup) {
            super(context);
            this.viewGroup = viewGroup;
            this.context = context;
            init();
    
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MyView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        private void init() {
            paint = new Paint();
            paint.setColor(context.getResources().getColor(R.color.gray));
            paint.setStrokeWidth(10);
            paint.setStyle(Paint.Style.FILL);
    
            for (int i = 0; i < viewGroup.getChildCount(); ++i) {
                View child = viewGroup.getChildAt(i);
    
                Rect rect = new Rect();
                int x = (int) child.getX() - dpToPx(20);
                int y = (int) child.getY() - dpToPx(20);
                rect.left = x;
                rect.top = y;
                rect.right = x + child.getWidth();
                rect.bottom = y + child.getHeight();
                rectList.add(rect);
            }
    
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            for (Rect rect : rectList) {
                canvas.drawRect(rect, paint);
            }
        }
        public static int dpToPx(int dp) {
            return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
        }
    
    }
    

    My MainActivity:

    ConstraintLayout constraintLayout;
        ImageView imageView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            constraintLayout = findViewById(R.id.parent_view);
            imageView = findViewById(R.id.item_profile_img);
    
            constraintLayout.post(() -> {
                
                ShimmerFrameLayout shimmerFrameLayout = new ShimmerFrameLayout(MainActivity.this);
    
                Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
                        .setDuration(2000)
                        .setBaseAlpha(0.9f)
                        .setHighlightAlpha(0.93f)
                        .setWidthRatio(1.5f)
                        .setDirection(Shimmer.Direction.RIGHT_TO_LEFT)
                        .setAutoStart(true)
                        .setBaseColor(getColor(android.R.color.darker_gray))
                        .setBaseColor(getColor(android.R.color.darker_gray))
                        .setHighlightColor(getColor(android.R.color.white))
                        .build();
                shimmerFrameLayout.setShimmer(shimmer);
    
                shimmerFrameLayout.addView(new MyView(MainActivity.this,constraintLayout));
                constraintLayout.addView(shimmerFrameLayout);
                shimmerFrameLayout.startShimmer();
            });
    
        }
    

    My activity_main.xml:

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:ignore="HardcodedText,MissingConstraints"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            tools:ignore="HardcodedText,MissingConstraints"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:id="@+id/parent_view">
    
            <ImageView
                android:id="@+id/item_profile_img"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/ic_launcher_background"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="ContentDescription,MissingConstraints" />
    
            <TextView
                android:id="@+id/item_student_name_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:text="Student name"
                android:textStyle="bold"
                app:layout_constraintStart_toEndOf="@+id/item_profile_img" />
    
            <TextView
                android:id="@+id/item_student_college"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginTop="8dp"
                android:text="Student college"
                app:layout_constraintStart_toEndOf="@+id/item_profile_img"
                app:layout_constraintTop_toBottomOf="@+id/item_student_name_title" />
    
            <TextView
                android:id="@+id/item_student_specialization"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginTop="8dp"
                android:text="Student specialization"
                app:layout_constraintStart_toEndOf="@+id/item_profile_img"
                app:layout_constraintTop_toBottomOf="@+id/item_student_college" />
    
            <TextView
                android:id="@+id/item_student_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginTop="8dp"
                android:text="Student description"
                app:layout_constraintTop_toBottomOf="@+id/item_profile_img"
                tools:ignore="MissingConstraints" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>