您的位置:首页 > 移动开发 > Android开发

Android 图片处理工具类汇总

2015-11-11 14:27 441 查看
很有用的Android图片处理工具,实现各种图片处理效果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

/*******图片加载与保存*******/

/**

* 以最省内存的方式读取本地资源的图片

*

* @param context

* @param resId

* @return

*/

public Bitmap readBitMap(int resId) {

BitmapFactory.Options opt = new BitmapFactory.Options();

opt.inPreferredConfig = Bitmap.Config.RGB_565;

opt.inPurgeable = true;

opt.inInputShareable = true;

// 获取资源图片

InputStream is = getResources().openRawResource(resId);

return BitmapFactory.decodeStream(is, null, opt);

}

/**

* 节省内存

*

* @Description:

* @param filePath

* @param outWidth

* @param outHeight

* @return

*/

public static Bitmap readBitmapAutoSize(String filePath, int outWidth, int outHeight) {

// outWidth和outHeight是目标图片的最大宽度和高度,用作限制

FileInputStream fs = null;

BufferedInputStream bs = null;

try {

fs = new FileInputStream(filePath);

bs = new BufferedInputStream(fs);

BitmapFactory.Options options = setBitmapOption(filePath, outWidth, outHeight);

return BitmapFactory.decodeStream(bs, null, options);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

bs.close();

fs.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

//保存图片到本地路径

public boolean saveBitmap(Bitmap bitmap, String fileName, String path) {

File file = new File(path);

FileOutputStream fos = null;

if (!file.exists()) {

file.mkdir();

}

File imageFile = new File(file, fileName);

try {

imageFile.createNewFile();

fos = new FileOutputStream(imageFile);

bitmap.compress(CompressFormat.JPEG, 50, fos);

fos.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if(fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

fos = null;

}

}

return true;

}

// 从view得到bitmap

public Bitmap getViewBitmap(View view) {

Bitmap bitmap = null;

try {

int width = view.getWidth();

int height = view.getHeight();

if (width != 0 && height != 0) {

bitmap = Bitmap.createBitmap(width, height,

Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

view.draw(canvas);

}

} catch (Exception e) {

bitmap = null;

Debug.out(e);

}

return bitmap;

}

/**

* @Title: getLoacalBitmap

* @Description: 加载本地图片

* @param @param url 本地路径

* @param @return

* @return Bitmap

* @throws

*/

public Bitmap getLoacalBitmap(String url) {

if (url != null) {

FileInputStream fis = null;

try {

fis = new FileInputStream(url);

return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

} finally {

StreamService.close(fis);

if(fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

fis = null;

}

}

} else {

return null;

}

}

/**

* 通过URL地址获取Bitmap对象

*

* @Title: getBitMapByUrl

* @param @param url

* @param @return

* @param @throws Exception

* @return Bitmap

* @throws

*/

public Bitmap getBitMapByUrl(final String url) {

URL fileUrl = null;

InputStream is = null;

Bitmap bitmap = null;

try {

fileUrl = new URL(url);

HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();

conn.setDoInput(true);

conn.connect();

is = conn.getInputStream();

bitmap = BitmapFactory.decodeStream(is);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (null != is) {

is.close();

}

} catch (IOException e) {

e.printStackTrace();

}

is = null;

}

return bitmap;

}

/*******获取图片信息*******/

/**

* @brief 从文件载入只获边框的,从返回的Options.outWidth和Options.outHight里取出即可

* @see android.graphics.BitmapFactory.Options#inJustDecodeBounds

*/

public Options loadJustDecodeBounds(String path) {

Options opts = new Options();

opts.inJustDecodeBounds = true;

loadFromFile(path, opts);

return opts;

}

/**

* @brief 读取图片方向信息

* @param path 图片路径

* @return 角度

*/

public int readPhotoDegree(String path) {

int degree = 0;

try {

ExifInterface exifInterface = new ExifInterface(path);

int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,

ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {

case ExifInterface.ORIENTATION_ROTATE_90:

degree = 90;

break;

case ExifInterface.ORIENTATION_ROTATE_180:

degree = 180;

break;

case ExifInterface.ORIENTATION_ROTATE_270:

degree = 270;

break;

default:

degree = 0;

}

} catch (IOException e) {

e.printStackTrace();

}

return degree;

}

/*******图片转换*******/

public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ?

Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

//canvas.setBitmap(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

private byte[] Bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

private Bitmap Bytes2Bimap(byte[] b) {

if(b.length != 0) {

return BitmapFactory.decodeByteArray(b, 0, b.length);

} else {

return null;

}

}

//Stream转换成Byte

static byte[] streamToBytes(InputStream is) {

ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

byte[] buffer = new byte[1024];

int len;

try {

while ((len = is.read(buffer)) >= 0) {

os.write(buffer, 0, len);

}

} catch (java.io.IOException e) {

}

return os.toByteArray();

}

/** Bitmap 格式转换

*

* @param src

* 需要重新编码的Bitmap

*

* @param format

* 编码后的格式(目前只支持png和jpeg这两种格式)

*

* @param quality

* 重新生成后的bitmap的质量

*

* @return

* 返回重新生成后的bitmap

*/

private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,

int quality) {

ByteArrayOutputStream os = new ByteArrayOutputStream();

src.compress(format, quality, os);

byte[] array = os.toByteArray();

return BitmapFactory.decodeByteArray(array, 0, array.length);

}

/*******图片缩放剪裁和形变*******/

/**

* 放大缩小图片,不保证宽高比

*

* @Title: zoomBitmap

* @param @param bitmap

* @param @param w

* @param @param h

* @return Bitmap

* @throws

*/

public Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidht = ((float) w / width);

float scaleHeight = ((float) h / height);

matrix.postScale(scaleWidht, scaleHeight);

Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

bitmap.recycle();

bitmap = null;

return newbmp;

}

/**

* @brief 缩放Bitmap

* @param src 源Bitmap

* @param dstWidth 目标宽度

* @param dstHeight 目标高度

* @param isRecycle 是否回收原图像

* @return Bitmap

*/

public Bitmap scaleBitmap(Bitmap src, int dstWidth, int dstHeight, boolean isRecycle) {

if (src.getWidth() == dstWidth && src.getHeight() == dstHeight) {

return src;

}

Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);

if (isRecycle && dst != src) {

src.recycle();

}

return dst;

}

//放大缩小图片,生成缩略图

public Bitmap extractThumbnail(Bitmap src, int width, int height) {

return ThumbnailUtils.extractThumbnail(src, width, height,

ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

}

/**

* @brief 裁剪Bitmap

* @param src 源Bitmap

* @param x 开始x坐标

* @param y 开始y坐标

* @param width 截取宽度

* @param height 截取高度

* @param isRecycle 是否回收原图像

* @return Bitmap

*/

public Bitmap cropBitmap(Bitmap src, int x, int y, int width, int height, boolean isRecycle) {

if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight()) {

return src;

}

Bitmap dst = Bitmap.createBitmap(src, x, y, width, height);

if (isRecycle && dst != src) {

src.recycle();

}

return dst;

}

/***

* 图片分割

*

* @param g

* :画布

* @param paint

* :画笔

* @param imgBit

* :图片

* @param x

* :X轴起点坐标

* @param y

* :Y轴起点坐标

* @param w

* :单一图片的宽度

* @param h

* :单一图片的高度

* @param line

* :第几列

* @param row

* :第几行

*/

public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,

int y, int w, int h, int line, int row) {

g.clipRect(x, y, x + w, h + y);

g.drawBitmap(imgBit, x – line * w, y – row * h, paint);

g.restore();

}

//将一个图片切割成多个图片

//传入的参数是要切割的Bitmap对象,和横向和竖向的切割片数

public class ImageSplitter {

public static List<ImagePiece> split(Bitmap bitmap, int xPiece, int yPiece) {

List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);

int width = bitmap.getWidth();

int height = bitmap.getHeight();

int pieceWidth = width / 3;

int pieceHeight = height / 3;

for (int i = 0; i < yPiece; i++) {

for (int j = 0; j < xPiece; j++) {

ImagePiece piece = new ImagePiece();

piece.index = j + i * xPiece;

int xValue = j * pieceWidth;

int yValue = i * pieceHeight;

piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,

pieceWidth, pieceHeight);

pieces.add(piece);

}

}

return pieces;

}

}

//图片翻转

Resources res = this.getContext().getResources();

img = BitmapFactory.decodeResource(res, R.drawable.slogo);

Matrix matrix = new Matrix();

matrix.postRotate(90);

/*翻转90度*/

int width = img.getWidth();

int height = img.getHeight();

r_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);

/**

* @brief 旋转Bitmap,顺时针

* @param src 源Bitmap

* @param degree 旋转角度

* @param isRecycle 是否回收原图像

* @return Bitmap

*/

public Bitmap rotateBitmap(Bitmap src, int degree, boolean isRecycle) {

if (degree % 360 == 0) {

return src;

}

int w = src.getWidth();

int h = src.getHeight();

Matrix matrix = new Matrix();

matrix.postRotate(degree);

Bitmap dst = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);

if (isRecycle && dst != src) {

src.recycle();

}

return dst;

}

/*******图片效果处理*******/

/**

* 图片透明度处理

*

* @param sourceImg

* 原始图片

* @param number

* 透明度

* @return

*/

public static Bitmap setAlpha(Bitmap sourceImg, int number) {

int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight()); // 获得图片的ARGB值

number = number * 255 / 100;

for (int i = 0; i < argb.length; i++) {

argb = (number << 24) | (argb & 0×00FFFFFF);// 修改最高2位的值

}

sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);

return sourceImg;

}

public class ImgService {

//亮

public final float[] LIGHT_ARR = new float[] {

1, 0, 0, 0, 100,

0, 1, 0, 0, 100,

0, 0, 1, 0, 100,

0, 0, 0, 1, 0

};

//暗

public final float[] DARK_ARR = new float[] {

0.2f, 0, 0, 0, 50.8f,

0, 0.2f, 0, 0, 50.8f,

0, 0, 0.2f, 0, 50.8f,

0, 0, 0, 1f, 0

};

//高对比

public final float[] GDB_ARR = new float[] {

5, 0, 0, 0, -250,

0, 5, 0, 0, -250,

0, 0, 5, 0, -250,

0, 0, 0, 1, 0

};

//高对比

public final float[] DDB_ARR = new float[] {

0.2f, 0, 0, 0, 50,

0, 0.2f, 0, 0, 50,

0, 0, 0.2f, 0, 50,

0, 0, 0, 1, 0

};

//高饱和

public final float[] GBH_ARR = new float[] {

3f, -1.8f, -0.25f, 0, 50,

-0.9f, 2.1f, -0.25f, 0, 50,

-0.9f, -1.8f, 3.8f, 0, 50,

0, 0, 0, 1, 0

};

//低饱和

public final float[] DBH_ARR = new float[] {

0.3f, 0.6f, 0.08f, 0, 0,

0.3f, 0.6f, 0.08f, 0, 0,

0.3f, 0.6f, 0.08f, 0, 0,

0, 0, 0, 1, 0

};

//COPY

public final float[] COPY_ARR = new float[] {

0, 0, 0, 0, 0,

0, 0, 0, 0, 0,

0, 0, 0, 0, 0,

0, 0, 0, 0, 0

};

}

/**

* 为图片加滤镜特效.array参数为ImgService定义的几个滤镜矩阵.如ImgService.LIGHT_ARR

* @param bmpOriginal

* @param array

* @return

*/

public Bitmap toGrayscale(Bitmap bmpOriginal, float[] array) {

int width, height;

height = bmpOriginal.getHeight();

width = bmpOriginal.getWidth();

Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,

Bitmap.Config.RGB_565);

Canvas c = new Canvas(bmpGrayscale);

Paint paint = new Paint();

ColorMatrix colorMatrix = new ColorMatrix();

colorMatrix.set(array);

paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

c.drawBitmap(bmpOriginal, 0, 0, paint);

bmpOriginal.recycle();

bmpOriginal = null;

return bmpGrayscale;

}

// Bitmap加水印

public Bitmap addWatermark(Bitmap src, Bitmap watermark) {

if (src == null || watermark == null) {

return src;

}

int sWid = src.getWidth();

int sHei = src.getHeight();

int wWid = watermark.getWidth();

int wHei = watermark.getHeight();

if (sWid == 0 || sHei == 0) {

return null;

}

if (sWid < wWid || sHei < wHei) {

return src;

}

Bitmap bitmap = Bitmap.createBitmap(sWid, sHei, Config.ARGB_8888);//Config可修改,改变内存占用

try {

Canvas cv = new Canvas(bitmap);

cv.drawBitmap(src, 0, 0, null);

cv.drawBitmap(watermark, sWid - wWid - 5, sHei - wHei - 5, null);

cv.save(Canvas.ALL_SAVE_FLAG);

cv.restore();

} catch (Exception e) {

bitmap = null;

e.getStackTrace();

} finally {

src.recycle();

src = null;

watermark.recycle();

watermark = null;

}

return bitmap;

}

/**

* 获得圆角图片

*

* @Description:

* @param bitmap

* @param roundPx

* @return

*/

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

int w = bitmap.getWidth();

int h = bitmap.getHeight();

Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xff424242;

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, w, h);

final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);

canvas.drawARGB(0, 0, 0, 0);

paint.setColor(color);

canvas.drawRoundRect(rectF, 10, 10, paint);// 圆角平滑度为10

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);

return output;

}

/***

* 绘制带有边框的文字

*

* @param strMsg

* :绘制内容

* @param g

* :画布

* @param paint

* :画笔

* @param setx

* ::X轴起始坐标

* @param sety

* :Y轴的起始坐标

* @param fg

* :前景色

* @param bg

* :背景色

*/

public void drawText(String strMsg, Canvas g, Paint paint, int setx,

int sety, int fg, int bg) {

paint.setColor(bg);

g.drawText(strMsg, setx + 1, sety, paint);

g.drawText(strMsg, setx, sety – 1, paint);

g.drawText(strMsg, setx, sety + 1, paint);

g.drawText(strMsg, setx – 1, sety, paint);

paint.setColor(fg);

g.drawText(strMsg, setx, sety, paint);

g.restore();

}

//获得带倒影的图片方法

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {

final int reflectionGap = 4;

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(bitmap, 0, 0, null);

Paint deafalutPaint = new Paint();

canvas.drawRect(0, height, width, height + reflectionGap,

deafalutPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();

LinearGradient shader = new LinearGradient(0,

bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

return bitmapWithReflection;

}

//Android Matrix类实现镜像倒影方法

public void drawRegion(Image image_src,

int x_src, int y_src,

int width, int height,

int transform,

int x_dest, int y_dest,

int anchor) {

if((anchor & VCENTER) != 0) {

y_dest -= height / 2;

} else if((anchor & BOTTOM) != 0) {

y_dest -= height;

}

if((anchor & RIGHT) != 0) {

x_dest -= width;

} else if((anchor & HCENTER) != 0) {

x_dest -= width / 2;

}

Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);

Matrix mMatrix = new Matrix();

Matrix temp = new Matrix();

Matrix temp2 = new Matrix();

float[] mirrorY = {

-1, 0, 0,

0, 1, 0,

0, 0, 1

};

temp.setValues(mirrorY);

switch(transform) {

case Sprite.TRANS_NONE:

break;

case Sprite.TRANS_ROT90:

mMatrix.setRotate(90, width / 2, height / 2);

break;

case Sprite.TRANS_ROT180:

mMatrix.setRotate(180, width / 2, height / 2);

break;

case Sprite.TRANS_ROT270:

mMatrix.setRotate(270, width / 2, height / 2);

break;

case Sprite.TRANS_MIRROR:

mMatrix.postConcat(temp);

break;

case Sprite.TRANS_MIRROR_ROT90:

mMatrix.postConcat(temp);

mMatrix.setRotate(90, width / 2, height / 2);

break;

case Sprite.TRANS_MIRROR_ROT180:

mMatrix.postConcat(temp);

mMatrix.setRotate(180, width / 2, height / 2);

break;

case Sprite.TRANS_MIRROR_ROT270:

mMatrix.postConcat(temp);

mMatrix.setRotate(270, width / 2, height / 2);

break;

}

mMatrix.setTranslate(x_dest, y_dest);

canvas.drawBitmap(newMap, mMatrix, mPaint);

}

/**

* 将彩色图转换为灰度图

* @param img 位图

* @return 返回转换好的位图

*/

public Bitmap convertGreyImg(Bitmap img) {

int width = img.getWidth(); //获取位图的宽

int height = img.getHeight(); //获取位图的高

int []pixels = new int[width * height]; //通过位图的大小创建像素点数组

img.getPixels(pixels, 0, width, 0, 0, width, height);

int alpha = 0xFF << 24;

for(int i = 0; i < height; i++) {

for(int j = 0; j < width; j++) {

int grey = pixels[width * i + j];

int red = ((grey & 0x00FF0000 ) >> 16);

int green = ((grey & 0x0000FF00) >> 8);

int blue = (grey & 0x000000FF);

grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);

grey = alpha | (grey << 16) | (grey << 8) | grey;

pixels[width * i + j] = grey;

}

}

Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);

result.setPixels(pixels, 0, width, 0, 0, width, height);

return result;

}

//压缩图片大小

public static Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

while ( baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中

options -= 10;//每次都减少10

}

ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中

Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片

return bitmap;

}

//同一张图片在两个不同的地方用到,但是两处的效果不一样。

//调用mutate()方法,可以使Drawable对象生成不同的constantstate对象,修改时就不会影响其它drawable对象的状态

//例如:通讯软件里不同用户,用同一个头像,一个要亮的,代表在线,一个要变灰,代表离线

Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);

//Make this drawable mutable.

//A mutable drawable is guaranteed to not share its state with any other drawable.

mDrawable.mutate();

ColorMatrix cm = new ColorMatrix();

cm.setSaturation(0);

ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);

mDrawable.setColorFilter(cf);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

/*******图片加载与保存*******/

/**

* 以最省内存的方式读取本地资源的图片

*

* @param context

* @param resId

* @return

*/

public Bitmap readBitMap(int resId) {

BitmapFactory.Options opt = new BitmapFactory.Options();

opt.inPreferredConfig = Bitmap.Config.RGB_565;

opt.inPurgeable = true;

opt.inInputShareable = true;

// 获取资源图片

InputStream is = getResources().openRawResource(resId);

return BitmapFactory.decodeStream(is, null, opt);

}

/**

* 节省内存

*

* @Description:

* @param filePath

* @param outWidth

* @param outHeight

* @return

*/

public static Bitmap readBitmapAutoSize(String filePath, int outWidth, int outHeight) {

// outWidth和outHeight是目标图片的最大宽度和高度,用作限制

FileInputStream fs = null;

BufferedInputStream bs = null;

try {

fs = new FileInputStream(filePath);

bs = new BufferedInputStream(fs);

BitmapFactory.Options options = setBitmapOption(filePath, outWidth, outHeight);

return BitmapFactory.decodeStream(bs, null, options);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

bs.close();

fs.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

//保存图片到本地路径

public boolean saveBitmap(Bitmap bitmap, String fileName, String path) {

File file = new File(path);

FileOutputStream fos = null;

if (!file.exists()) {

file.mkdir();

}

File imageFile = new File(file, fileName);

try {

imageFile.createNewFile();

fos = new FileOutputStream(imageFile);

bitmap.compress(CompressFormat.JPEG, 50, fos);

fos.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if(fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

fos = null;

}

}

return true;

}

// 从view得到bitmap

public Bitmap getViewBitmap(View view) {

Bitmap bitmap = null;

try {

int width = view.getWidth();

int height = view.getHeight();

if (width != 0 && height != 0) {

bitmap = Bitmap.createBitmap(width, height,

Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

view.draw(canvas);

}

} catch (Exception e) {

bitmap = null;

Debug.out(e);

}

return bitmap;

}

/**

* @Title: getLoacalBitmap

* @Description: 加载本地图片

* @param @param url 本地路径

* @param @return

* @return Bitmap

* @throws

*/

public Bitmap getLoacalBitmap(String url) {

if (url != null) {

FileInputStream fis = null;

try {

fis = new FileInputStream(url);

return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

} finally {

StreamService.close(fis);

if(fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

fis = null;

}

}

} else {

return null;

}

}

/**

* 通过URL地址获取Bitmap对象

*

* @Title: getBitMapByUrl

* @param @param url

* @param @return

* @param @throws Exception

* @return Bitmap

* @throws

*/

public Bitmap getBitMapByUrl(final String url) {

URL fileUrl = null;

InputStream is = null;

Bitmap bitmap = null;

try {

fileUrl = new URL(url);

HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();

conn.setDoInput(true);

conn.connect();

is = conn.getInputStream();

bitmap = BitmapFactory.decodeStream(is);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (null != is) {

is.close();

}

} catch (IOException e) {

e.printStackTrace();

}

is = null;

}

return bitmap;

}

/*******获取图片信息*******/

/**

* @brief 从文件载入只获边框的,从返回的Options.outWidth和Options.outHight里取出即可

* @see android.graphics.BitmapFactory.Options#inJustDecodeBounds

*/

public Options loadJustDecodeBounds(String path) {

Options opts = new Options();

opts.inJustDecodeBounds = true;

loadFromFile(path, opts);

return opts;

}

/**

* @brief 读取图片方向信息

* @param path 图片路径

* @return 角度

*/

public int readPhotoDegree(String path) {

int degree = 0;

try {

ExifInterface exifInterface = new ExifInterface(path);

int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,

ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {

case ExifInterface.ORIENTATION_ROTATE_90:

degree = 90;

break;

case ExifInterface.ORIENTATION_ROTATE_180:

degree = 180;

break;

case ExifInterface.ORIENTATION_ROTATE_270:

degree = 270;

break;

default:

degree = 0;

}

} catch (IOException e) {

e.printStackTrace();

}

return degree;

}

/*******图片转换*******/

public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ?

Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

//canvas.setBitmap(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

private byte[] Bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

private Bitmap Bytes2Bimap(byte[] b) {

if(b.length != 0) {

return BitmapFactory.decodeByteArray(b, 0, b.length);

} else {

return null;

}

}

//Stream转换成Byte

static byte[] streamToBytes(InputStream is) {

ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

byte[] buffer = new byte[1024];

int len;

try {

while ((len = is.read(buffer)) >= 0) {

os.write(buffer, 0, len);

}

} catch (java.io.IOException e) {

}

return os.toByteArray();

}

/** Bitmap 格式转换

*

* @param src

* 需要重新编码的Bitmap

*

* @param format

* 编码后的格式(目前只支持png和jpeg这两种格式)

*

* @param quality

* 重新生成后的bitmap的质量

*

* @return

* 返回重新生成后的bitmap

*/

private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,

int quality) {

ByteArrayOutputStream os = new ByteArrayOutputStream();

src.compress(format, quality, os);

byte[] array = os.toByteArray();

return BitmapFactory.decodeByteArray(array, 0, array.length);

}

/*******图片缩放剪裁和形变*******/

/**

* 放大缩小图片,不保证宽高比

*

* @Title: zoomBitmap

* @param @param bitmap

* @param @param w

* @param @param h

* @return Bitmap

* @throws

*/

public Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidht = ((float) w / width);

float scaleHeight = ((float) h / height);

matrix.postScale(scaleWidht, scaleHeight);

Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

bitmap.recycle();

bitmap = null;

return newbmp;

}

/**

* @brief 缩放Bitmap

* @param src 源Bitmap

* @param dstWidth 目标宽度

* @param dstHeight 目标高度

* @param isRecycle 是否回收原图像

* @return Bitmap

*/

public Bitmap scaleBitmap(Bitmap src, int dstWidth, int dstHeight, boolean isRecycle) {

if (src.getWidth() == dstWidth && src.getHeight() == dstHeight) {

return src;

}

Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);

if (isRecycle && dst != src) {

src.recycle();

}

return dst;

}

//放大缩小图片,生成缩略图

public Bitmap extractThumbnail(Bitmap src, int width, int height) {

return ThumbnailUtils.extractThumbnail(src, width, height,

ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

}

/**

* @brief 裁剪Bitmap

* @param src 源Bitmap

* @param x 开始x坐标

* @param y 开始y坐标

* @param width 截取宽度

* @param height 截取高度

* @param isRecycle 是否回收原图像

* @return Bitmap

*/

public Bitmap cropBitmap(Bitmap src, int x, int y, int width, int height, boolean isRecycle) {

if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight()) {

return src;

}

Bitmap dst = Bitmap.createBitmap(src, x, y, width, height);

if (isRecycle && dst != src) {

src.recycle();

}

return dst;

}

/***

* 图片分割

*

* @param g

* :画布

* @param paint

* :画笔

* @param imgBit

* :图片

* @param x

* :X轴起点坐标

* @param y

* :Y轴起点坐标

* @param w

* :单一图片的宽度

* @param h

* :单一图片的高度

* @param line

* :第几列

* @param row

* :第几行

*/

public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,

int y, int w, int h, int line, int row) {

g.clipRect(x, y, x + w, h + y);

g.drawBitmap(imgBit, x – line * w, y – row * h, paint);

g.restore();

}

//将一个图片切割成多个图片

//传入的参数是要切割的Bitmap对象,和横向和竖向的切割片数

public class ImageSplitter {

public static List<ImagePiece> split(Bitmap bitmap, int xPiece, int yPiece) {

List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);

int width = bitmap.getWidth();

int height = bitmap.getHeight();

int pieceWidth = width / 3;

int pieceHeight = height / 3;

for (int i = 0; i < yPiece; i++) {

for (int j = 0; j < xPiece; j++) {

ImagePiece piece = new ImagePiece();

piece.index = j + i * xPiece;

int xValue = j * pieceWidth;

int yValue = i * pieceHeight;

piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,

pieceWidth, pieceHeight);

pieces.add(piece);

}

}

return pieces;

}

}

//图片翻转

Resources res = this.getContext().getResources();

img = BitmapFactory.decodeResource(res, R.drawable.slogo);

Matrix matrix = new Matrix();

matrix.postRotate(90);

/*翻转90度*/

int width = img.getWidth();

int height = img.getHeight();

r_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);

/**

* @brief 旋转Bitmap,顺时针

* @param src 源Bitmap

* @param degree 旋转角度

* @param isRecycle 是否回收原图像

* @return Bitmap

*/

public Bitmap rotateBitmap(Bitmap src, int degree, boolean isRecycle) {

if (degree % 360 == 0) {

return src;

}

int w = src.getWidth();

int h = src.getHeight();

Matrix matrix = new Matrix();

matrix.postRotate(degree);

Bitmap dst = Bitmap.createBitmap(src, 0, 0, w, h, matrix, true);

if (isRecycle && dst != src) {

src.recycle();

}

return dst;

}

/*******图片效果处理*******/

/**

* 图片透明度处理

*

* @param sourceImg

* 原始图片

* @param number

* 透明度

* @return

*/

public static Bitmap setAlpha(Bitmap sourceImg, int number) {

int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight()); // 获得图片的ARGB值

number = number * 255 / 100;

for (int i = 0; i < argb.length; i++) {

argb[i] = (number << 24) | (argb & 0×00FFFFFF);// 修改最高2[i]位的值

}

sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);

return sourceImg;

}

public class ImgService {

//亮

public final float[] LIGHT_ARR = new float[] {

1, 0, 0, 0, 100,

0, 1, 0, 0, 100,

0, 0, 1, 0, 100,

0, 0, 0, 1, 0

};

//暗

public final float[] DARK_ARR = new float[] {

0.2f, 0, 0, 0, 50.8f,

0, 0.2f, 0, 0, 50.8f,

0, 0, 0.2f, 0, 50.8f,

0, 0, 0, 1f, 0

};

//高对比

public final float[] GDB_ARR = new float[] {

5, 0, 0, 0, -250,

0, 5, 0, 0, -250,

0, 0, 5, 0, -250,

0, 0, 0, 1, 0

};

//高对比

public final float[] DDB_ARR = new float[] {

0.2f, 0, 0, 0, 50,

0, 0.2f, 0, 0, 50,

0, 0, 0.2f, 0, 50,

0, 0, 0, 1, 0

};

//高饱和

public final float[] GBH_ARR = new float[] {

3f, -1.8f, -0.25f, 0, 50,

-0.9f, 2.1f, -0.25f, 0, 50,

-0.9f, -1.8f, 3.8f, 0, 50,

0, 0, 0, 1, 0

};

//低饱和

public final float[] DBH_ARR = new float[] {

0.3f, 0.6f, 0.08f, 0, 0,

0.3f, 0.6f, 0.08f, 0, 0,

0.3f, 0.6f, 0.08f, 0, 0,

0, 0, 0, 1, 0

};

//COPY

public final float[] COPY_ARR = new float[] {

0, 0, 0, 0, 0,

0, 0, 0, 0, 0,

0, 0, 0, 0, 0,

0, 0, 0, 0, 0

};

}

/**

* 为图片加滤镜特效.array参数为ImgService定义的几个滤镜矩阵.如ImgService.LIGHT_ARR

* @param bmpOriginal

* @param array

* @return

*/

public Bitmap toGrayscale(Bitmap bmpOriginal, float[] array) {

int width, height;

height = bmpOriginal.getHeight();

width = bmpOriginal.getWidth();

Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,

Bitmap.Config.RGB_565);

Canvas c = new Canvas(bmpGrayscale);

Paint paint = new Paint();

ColorMatrix colorMatrix = new ColorMatrix();

colorMatrix.set(array);

paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

c.drawBitmap(bmpOriginal, 0, 0, paint);

bmpOriginal.recycle();

bmpOriginal = null;

return bmpGrayscale;

}

// Bitmap加水印

public Bitmap addWatermark(Bitmap src, Bitmap watermark) {

if (src == null || watermark == null) {

return src;

}

int sWid = src.getWidth();

int sHei = src.getHeight();

int wWid = watermark.getWidth();

int wHei = watermark.getHeight();

if (sWid == 0 || sHei == 0) {

return null;

}

if (sWid < wWid || sHei < wHei) {

return src;

}

Bitmap bitmap = Bitmap.createBitmap(sWid, sHei, Config.ARGB_8888);//Config可修改,改变内存占用

try {

Canvas cv = new Canvas(bitmap);

cv.drawBitmap(src, 0, 0, null);

cv.drawBitmap(watermark, sWid - wWid - 5, sHei - wHei - 5, null);

cv.save(Canvas.ALL_SAVE_FLAG);

cv.restore();

} catch (Exception e) {

bitmap = null;

e.getStackTrace();

} finally {

src.recycle();

src = null;

watermark.recycle();

watermark = null;

}

return bitmap;

}

/**

* 获得圆角图片

*

* @Description:

* @param bitmap

* @param roundPx

* @return

*/

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

int w = bitmap.getWidth();

int h = bitmap.getHeight();

Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xff424242;

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, w, h);

final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);

canvas.drawARGB(0, 0, 0, 0);

paint.setColor(color);

canvas.drawRoundRect(rectF, 10, 10, paint);// 圆角平滑度为10

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);

return output;

}

/***

* 绘制带有边框的文字

*

* @param strMsg

* :绘制内容

* @param g

* :画布

* @param paint

* :画笔

* @param setx

* ::X轴起始坐标

* @param sety

* :Y轴的起始坐标

* @param fg

* :前景色

* @param bg

* :背景色

*/

public void drawText(String strMsg, Canvas g, Paint paint, int setx,

int sety, int fg, int bg) {

paint.setColor(bg);

g.drawText(strMsg, setx + 1, sety, paint);

g.drawText(strMsg, setx, sety – 1, paint);

g.drawText(strMsg, setx, sety + 1, paint);

g.drawText(strMsg, setx – 1, sety, paint);

paint.setColor(fg);

g.drawText(strMsg, setx, sety, paint);

g.restore();

}

//获得带倒影的图片方法

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {

final int reflectionGap = 4;

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(bitmap, 0, 0, null);

Paint deafalutPaint = new Paint();

canvas.drawRect(0, height, width, height + reflectionGap,

deafalutPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();

LinearGradient shader = new LinearGradient(0,

bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

return bitmapWithReflection;

}

//Android Matrix类实现镜像倒影方法

public void drawRegion(Image image_src,

int x_src, int y_src,

int width, int height,

int transform,

int x_dest, int y_dest,

int anchor) {

if((anchor & VCENTER) != 0) {

y_dest -= height / 2;

} else if((anchor & BOTTOM) != 0) {

y_dest -= height;

}

if((anchor & RIGHT) != 0) {

x_dest -= width;

} else if((anchor & HCENTER) != 0) {

x_dest -= width / 2;

}

Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);

Matrix mMatrix = new Matrix();

Matrix temp = new Matrix();

Matrix temp2 = new Matrix();

float[] mirrorY = {

-1, 0, 0,

0, 1, 0,

0, 0, 1

};

temp.setValues(mirrorY);

switch(transform) {

case Sprite.TRANS_NONE:

break;

case Sprite.TRANS_ROT90:

mMatrix.setRotate(90, width / 2, height / 2);

break;

case Sprite.TRANS_ROT180:

mMatrix.setRotate(180, width / 2, height / 2);

break;

case Sprite.TRANS_ROT270:

mMatrix.setRotate(270, width / 2, height / 2);

break;

case Sprite.TRANS_MIRROR:

mMatrix.postConcat(temp);

break;

case Sprite.TRANS_MIRROR_ROT90:

mMatrix.postConcat(temp);

mMatrix.setRotate(90, width / 2, height / 2);

break;

case Sprite.TRANS_MIRROR_ROT180:

mMatrix.postConcat(temp);

mMatrix.setRotate(180, width / 2, height / 2);

break;

case Sprite.TRANS_MIRROR_ROT270:

mMatrix.postConcat(temp);

mMatrix.setRotate(270, width / 2, height / 2);

break;

}

mMatrix.setTranslate(x_dest, y_dest);

canvas.drawBitmap(newMap, mMatrix, mPaint);

}

/**

* 将彩色图转换为灰度图

* @param img 位图

* @return 返回转换好的位图

*/

public Bitmap convertGreyImg(Bitmap img) {

int width = img.getWidth(); //获取位图的宽

int height = img.getHeight(); //获取位图的高

int []pixels = new int[width * height]; //通过位图的大小创建像素点数组

img.getPixels(pixels, 0, width, 0, 0, width, height);

int alpha = 0xFF << 24;

for(int i = 0; i < height; i++) {

for(int j = 0; j < width; j++) {

int grey = pixels[width * i + j];

int red = ((grey & 0x00FF0000 ) >> 16);

int green = ((grey & 0x0000FF00) >> 8);

int blue = (grey & 0x000000FF);

grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);

grey = alpha | (grey << 16) | (grey << 8) | grey;

pixels[width * i + j] = grey;

}

}

Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);

result.setPixels(pixels, 0, width, 0, 0, width, height);

return result;

}

//压缩图片大小

public static Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

while ( baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中

options -= 10;//每次都减少10

}

ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中

Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片

return bitmap;

}

//同一张图片在两个不同的地方用到,但是两处的效果不一样。

//调用mutate()方法,可以使Drawable对象生成不同的constantstate对象,修改时就不会影响其它drawable对象的状态

//例如:通讯软件里不同用户,用同一个头像,一个要亮的,代表在线,一个要变灰,代表离线

Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);

//Make this drawable mutable.

//A mutable drawable is guaranteed to not share its state with any other drawable.

mDrawable.mutate();

ColorMatrix cm = new ColorMatrix();

cm.setSaturation(0);

ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);

mDrawable.setColorFilter(cf);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: