matrix自定义类
package com.example.day_3_2_juzhentupian;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class MyMatrix extends View {
Matrix matrix;
Paint paint;
Bitmap bitmap;
public MyMatrix(Context context) {
super(context);
init();
}
public MyMatrix(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
matrix=new Matrix();
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap,matrix,paint);
}
public void tran(){
matrix.reset();
matrix.setTranslate(400,400);
invalidate();
}
public void suofang(){
matrix.reset();
matrix.setScale(10, 10);
invalidate();
}
public void xuanzhuan(){
matrix.reset();
matrix.setRotate(40);
invalidate();
}
public void skew() {
matrix.reset();
matrix.setSkew(10, 10);
invalidate();
}
}
main1页面
package com.example.day_3_2_juzhentupian;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private MyMatrix img;
private Button translate;
private Button scale;
private Button rotate;
private Button skew;
private Button tiaozhuan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.tran();
}
});
scale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.suofang();
}
});
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.xuanzhuan();
}
});
skew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img.skew();
}
});
tiaozhuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
private void initView() {
img = (MyMatrix) findViewById(R.id.img);
translate = (Button) findViewById(R.id.translate);
scale = (Button) findViewById(R.id.scale);
rotate = (Button) findViewById(R.id.rotate);
skew = (Button) findViewById(R.id.skew);
tiaozhuan = (Button) findViewById(R.id.tiaozhuan);
}
}
main2页面
package com.example.day_3_2_juzhentupian;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.contrarywind.view.WheelView;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private WheelView wheelView;
private Button shijian;
private Button sanji;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
shijian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showTimePickerView();
}
});
sanji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initOptionPicker();
}
});
}
private void initView() {
wheelView = (WheelView) findViewById(R.id.wheel_view);
shijian = (Button) findViewById(R.id.shijian);
sanji = (Button) findViewById(R.id.sanji);
}
private void showTimePickerView() {
TimePickerView pvTime = new TimePickerBuilder(Main2Activity.this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
Toast.makeText(Main2Activity.this, date.toString(), Toast.LENGTH_SHORT).show();
}
}).build();
pvTime.show();//显示时间选择器
}
private void initOptionPicker() {
final List<String> options1Items = new ArrayList<>();
options1Items.add("河南省");
options1Items.add("河北省");
options1Items.add("北京市");
List<String> options2Items_01 = new ArrayList<>();
options2Items_01.add("洛阳市");
options2Items_01.add("郑州市");
options2Items_01.add("新乡");
options2Items_01.add("平顶山");
List<String> options2Items_02 = new ArrayList<>();
options2Items_02.add("唐山");
options2Items_02.add("廊坊");
options2Items_02.add("沧州");
List<String> options2Items_03 = new ArrayList<>();
options2Items_03.add("海淀区");
options2Items_03.add("房山区");
options2Items_03.add("密云区");
options2Items_03.add("朝阳区");
final List<List<String>> options2Items = new ArrayList<>();
options2Items.add(options2Items_01);
options2Items.add(options2Items_02);
options2Items.add(options2Items_03);
OptionsPickerView optionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
String tx = options1Items.get(options1)
+ options2Items.get(options1).get(options2);
Toast.makeText(getBaseContext(), tx, Toast.LENGTH_SHORT).show();
}
})
.setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
@Override
public void onOptionsSelectChanged(int options1, int options2, int options3) {
String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
Toast.makeText(Main2Activity.this, str, Toast.LENGTH_SHORT).show();
}
})
.build();
optionsPickerView.setPicker(options1Items, options2Items);
optionsPickerView.show();
}
}
相关布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/shijian"
android:text="时间"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sanji"
android:text="三级联动"
/>
<com.contrarywind.view.WheelView
android:id="@+id/wheel_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.day_3_1_juzhentupian.MyMatrix
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img"
>
</com.example.day_3_1_juzhentupian.MyMatrix>
<LinearLayout
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translate"
android:layout_weight="1.0"
android:id="@+id/translate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scale"
android:layout_weight="1.0"
android:id="@+id/scale" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotate"
android:layout_weight="1.0"
android:id="@+id/rotate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="skew"
android:layout_weight="1.0"
android:id="@+id/skew" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Sec"
android:layout_weight="1.0"
android:id="@+id/tiaozhuan"
/>
</LinearLayout>
</FrameLayout>
效果图片
效果分别为位移 放大 旋转 切分