一.PhotoView
1.倒入依赖
implementation ‘com.github.chrisbanes:PhotoView:2.0.0’
2.项目gradle添加maven仓库
allprojects {
repositories {
google()
jcenter()
maven {
url "https://raw.github.com/bmob/bmob-android-sdk/master"
}
maven {
url "https://jitpack.io"
}
}
}
3.布局文件
<?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:layout_height="match_parent"
tools:context=".matrix.PhotoViewActivity">
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/photo_view"
android:layout_width="match_parent"
android:src="@drawable/erha"
android:layout_height="match_parent"></com.github.chrisbanes.photoview.PhotoView>
</LinearLayout>
二.Matrix矩阵
- Matrix 就是矩阵的意思,我们通常使用它进行一些图片的变化操作
平移、缩放、旋转、错切、水印等效果 - Matrix 三种动作 pre 、post 、set
1.pre代表之前有操作,乖乖排队 2.post代表插队到第一个 3.set代表设置,替换之前所有
核心代码:
matrix.reset();
matrix.preTranslate(100, 100);
matrix.preScale(2,2);
matrix.preRotate(45);
matrix.preSkew(0,1);
invalidate();
全部代码:
public class MyView extends View {
private Paint paint;
private Matrix matrix;
public MyView(Context context) {
super(context);
initPaint();
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
private void initPaint() {
paint = new Paint();
matrix = new Matrix();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pao);
canvas.drawBitmap(bitmap, matrix, paint);
}
public void change() {
matrix.reset();
matrix.preTranslate(100, 100);
matrix.preScale(2,2);
matrix.preRotate(45);
matrix.preSkew(0,1);
invalidate();
}
}
public class Main4Activity extends AppCompatActivity {
@BindView(R.id.myview)
MyView myview;
@BindView(R.id.change)
Button change;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
ButterKnife.bind(this);
}
@OnClick({R.id.change})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.change:
myview.change();
break;
}
}
}
|