图片移动代码
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.administrator.mywidgetmode.Bitmapview.MyBitmapView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyBitmapViewActivity
package com.example.administrator.mywidgetmode.Bitmapview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.administrator.mywidgetmode.R;
/**
* Created by Administrator on 2015/9/17.
*/
public class MyBitmapViewActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bitmap);
}
}
MyBitmapView
package com.example.administrator.mywidgetmode.Bitmapview;
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.util.Log;
import android.view.View;
import com.example.administrator.mywidgetmode.R;
/**
* Created by Administrator on 2015/9/17.
*/
public class MyBitmapView extends View {
private int width;
private int height;
private Bitmap mBitmap;
private Paint mPaint;
private Matrix matrix;
private int mBitmapWidth;
private int mBitmapHeight;
public MyBitmapView(Context context) {
super(context);
}
public MyBitmapView(Context context, AttributeSet attrs) {
super(context, attrs);
mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
Log.d("bitmap", "图片相关信息高" + mBitmap.getWidth() + "宽" + mBitmap.getHeight());
mPaint = new Paint();
matrix = new Matrix();
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight = mBitmap.getHeight();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);//设置画布的大小,长和宽
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//缩放
matrix.reset();
matrix.postScale(2, 2);//高放大两倍,宽放大两倍
canvas.drawBitmap(mBitmap, matrix, mPaint);
//原图
matrix.reset();
canvas.drawBitmap(mBitmap, matrix, mPaint);
//y轴平移两个高
matrix.postTranslate(0, mBitmapHeight * 2);
canvas.drawBitmap(mBitmap, matrix, mPaint);
//旋转
matrix.reset();
matrix.postRotate(180);//旋转角度
matrix.postTranslate(mBitmapWidth * 2, mBitmapHeight * 3);//平移
canvas.drawBitmap(mBitmap, matrix, mPaint);
//拉伸
// matrix.reset();
// matrix.postSkew(0, 2);
// canvas.drawBitmap(mBitmap, matrix, mPaint);
//关于x轴对称
matrix.reset();
float matrix_values[] = {1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f};
matrix.setValues(matrix_values);
matrix.postTranslate(0, mBitmapHeight * 2);
canvas.drawBitmap(mBitmap, matrix, mPaint);
//关于y轴对称
matrix.reset();
float matrix_values2[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};
matrix.setValues(matrix_values2);
matrix.postTranslate(mBitmapWidth * 2, 0);
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
}
仿联系人边上字母列表代码
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.administrator.mywidgetmode.Slider.MySlider
android:id="@+id/myslider"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textview"
android:text="A"
android:textSize="100dp"
android:gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
MySliderActivity
package com.example.administrator.mywidgetmode.Slider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import com.example.administrator.mywidgetmode.R;
/**
* Created by Administrator on 2015/9/17.
*/
public class MySliderActivity extends AppCompatActivity {
private TextView mTextView;
private MySlider mySlider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider);
mTextView = (TextView) findViewById(R.id.textview);
mySlider = (MySlider) findViewById(R.id.myslider);
mySlider.setonItemSelectedListener(new MySlider.OnItemSelect() {
@Override
public void onItemSelected(int index, String indexString) {
mTextView.setText(indexString);
}
});
}
}
MySlider
package com.example.administrator.mywidgetmode.Slider;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Administrator on 2015/9/17.
*/
public class MySlider extends View {
private int width;
private int height;
private Paint mPaintText;
private Paint mPaintRed;
private int index=-1;
private String[] array = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
//观察者模式
private OnItemSelect listener;
public interface OnItemSelect{
public void onItemSelected(int index,String indexString);
}
public void setonItemSelectedListener(OnItemSelect listener) {
this.listener = listener;
}
public MySlider(Context context) {
super(context);
}
public MySlider(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintText = new Paint();
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintRed=new Paint();
mPaintRed.setColor(Color.RED);
mPaintRed.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);//设置画布的大小,长和宽
mPaintText.setTextSize(height / 26f);//设置文本字体大小
mPaintRed.setTextSize(height / 26f);
}
private float x;
private float y;
@Override//事件监听
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
if (x>width-mPaintText.measureText("m")*2){
index=(int)y/(height/26);
Log.d("onTouche", "点击到的按钮是" + array[index]);
if(listener!=null){
listener.onItemSelected(index,array[index]);
}
invalidate();//刷新
return true;
}
break;
case MotionEvent.ACTION_UP:
index=-1;
invalidate();
return true;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < 26; i++) {
if(index==i){
canvas.drawText(""+array[i],width-mPaintText.measureText("m"),height/26*(i+1),mPaintRed);
}else {
canvas.drawText("" + array[i], width - mPaintText.measureText("m"), height / 26 * (i + 1), mPaintText);
//mPaintText.measureText("m")文本的宽度
}
}
}
}