简体   繁体   中英

How to draw circle in android by pressing a button?

I want to show small dots on screen while pressing a button, something like pin code...

switch (view.getId()){
    case R.id.buttonNum1:
        editor.putString("PinNumbers",getString(R.string.numberOne));
        int x = 10;
        int y = 30;
        int r = 100;
        Paint mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        Canvas mCanvas = new Canvas();
        mCanvas.drawCircle(x,y,r,mPaint);
        break;

You can achive by creating ripple effect

create one drawable name like pin_button_ripple in drawable-v21 folder and add bellow code

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="?android:colorPrimary" />

        </shape>
    </item>
</ripple>

Now in Layout file set it as a background of button and define click listener in activity

 <Button android:id="@+id/btnPinButton" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="@drawable/pin_button_ripple" android:padding="20dp" android:text="10"/> 

You will see the ripple effect in circle shape like we enter Pin number or password via keyboard and showing that effect

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