Android learning notes

Keywords: Android xml encoding

The button changes the background as you press:

First write a selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Specify picture when pressed-->
    <item android:state_pressed="true"
        android:drawable="@drawable/r1"/>
    <!-- Specifies the picture when the button is released-->
    <item android:state_pressed="false"
        android:drawable="@drawable/r2"/>

</selector>

Then set the Button background to selector.xml.

Radio button and check box

RadioButton is usually used with RadioGroup

<RadioGroup
            android:id="@+id/rg"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            >
            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="male"/>
            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="false"
                android:text="female"/>

        </RadioGroup>

Toggle button and Switch

 <ToggleButton
                android:id="@+id/toggle"
                android:textOff="shut"
                android:textOn="open"
                android:checked="true"
                android:background="@drawable/r1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Switch
                android:id="@+id/switchTest"
                android:textOn="Happy"
                android:textOff="open"
                android:checked="true"
                android:text="test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

A new way to realize multiple button click events

//First, create the listener, and judge the id of different View objects, that is, different components in the listener        
View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.plus:
                        if (alpha < 255)
                        alpha += 20;break;

                    case R.id.minus:
                        if (alpha >20)
                        alpha -= 20;break;
                }
                image1.setImageAlpha(alpha);

            }
        };
//Finally, we are binding the listener and the button together.
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);

Use of ZoomControls:

zoomControls.setOnZoomInClickListener
zoomControls.setOnZoomOutClickListener

Use of ImageView:

 image1.setOnTouchListener(new View.OnTouchListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                a = event.getX();
                b = event.getY();
               change(a,b,zoom);
                return false;
            }
        });

Event is a touch event. Get the coordinates of the touch through getX(),getY().

public void change(float xx, float yy,int zoom){
        //Get the bitmap of the first picture display box
        //BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
        //bitmap = bitmapDrawable.getBitmap();
        //Scale of the actual size of the bitmap image to the first ImageView
        double scale = 1.0 * bitmap.getHeight() / image1.getHeight();
        //Get the starting point of the picture to be displayed
        //Round is round
        x = Math.round(xx * scale);
        y = Math.round(yy * scale);
        if (x + zoom > bitmap.getWidth()){
            x = bitmap.getWidth()- zoom;
        }
        if(y + zoom > bitmap.getHeight()){
            y = bitmap.getHeight() - zoom;
        }
        image2.setImageBitmap(Bitmap.createBitmap(bitmap,(int)x,(int)y,zoom,zoom));
        image2.setImageAlpha(alpha);
    }
Bitmap createBitmap( Bitmap source, int x, int y, int width, int height)

Source is the source graph, x and y are the starting coordinates, width and height are width and height. This function is ROI, which intercepts the region of interest.

Posted by 22Pixels on Mon, 18 Nov 2019 11:22:49 -0800