Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Created October 25, 2018 03:54
Show Gist options
  • Save arthtilva/f657d7b19b454c3fbf2ab02135b165da to your computer and use it in GitHub Desktop.
Save arthtilva/f657d7b19b454c3fbf2ab02135b165da to your computer and use it in GitHub Desktop.
package com.mfinity.doodlecamera.sticker;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader.TileMode;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.mfinity.doodlecamera.R;
import com.mfinity.doodlecamera.customview.AutoResizeTextView;
public class TextArtView extends RelativeLayout {
int baseh;
int basew;
int basex;
int basey;
Button btndel;
Button btnrot;
Button btnscl;
Button btnflip;
Activity cntx;
boolean freeze = false;
Button imgring;
RelativeLayout layBg;
RelativeLayout layGroup;
public LayoutInflater mInflater;
float margl;
float margt;
int pivx;
int pivy;
int[] size;
float startDegree;
public AutoResizeTextView textViewArt;
class C01121 implements OnCreateContextMenuListener {
C01121() {
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
}
}
class C011GONE2 implements OnTouchListener {
final GestureDetector gestureDetector;
class C01131 extends SimpleOnGestureListener {
C01131() {
}
public boolean onDoubleTap(MotionEvent e) {
if (!freeze) {
textViewArt.setFocusable(true);
textViewArt.setFocusableInTouchMode(true);
textViewArt.setClickable(true);
textViewArt.setSelected(true);
textViewArt.setCursorVisible(true);
if (textViewArt.getText().toString().equals(cntx.getString(R.string.doubletaptoedit))) {
textViewArt.setText("");
}
}
return false;
}
}
C011GONE2() {
gestureDetector = new GestureDetector(cntx, new C01131());
}
public boolean onTouch(View v, MotionEvent event) {
LayoutParams layoutParams = (LayoutParams) layGroup.getLayoutParams();
if (freeze || textViewArt.isSelected()) {
return false;
}
switch (event.getAction() & 255) {
case 0:
layBg = (RelativeLayout) getParent();
layGroup.performClick();
btndel.setVisibility(VISIBLE);
btnrot.setVisibility(VISIBLE);
btnscl.setVisibility(VISIBLE);
btnflip.setVisibility(VISIBLE);
imgring.setVisibility(VISIBLE);
layGroup.bringToFront();
basex = (int) (event.getRawX() - ((float) layoutParams.leftMargin));
basey = (int) (event.getRawY() - ((float) layoutParams.topMargin));
break;
case 2:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord - basex > (-((layGroup.getWidth() * 2) / 3)) && x_cord - basex < layBg.getWidth() - (layGroup.getWidth() / 3)) {
layoutParams.leftMargin = x_cord - basex;
}
if (y_cord - basey > (-((layGroup.getHeight() * 2) / 3)) && y_cord - basey < layBg.getHeight() - (layGroup.getHeight() / 3)) {
layoutParams.topMargin = y_cord - basey;
}
layoutParams.rightMargin = -1000;
layoutParams.bottomMargin = -1000;
layGroup.setLayoutParams(layoutParams);
break;
}
layGroup.invalidate();
return gestureDetector.onTouchEvent(event);
}
}
class C01153 implements OnTouchListener {
C01153() {
}
@SuppressLint({"NewApi"})
public boolean onTouch(View v, MotionEvent event) {
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
layBg = (RelativeLayout) getParent();
LayoutParams layoutParams = (LayoutParams) layGroup.getLayoutParams();
switch (event.getAction()) {
case 0:
basex = x_cord;
basey = y_cord;
basew = layGroup.getWidth();
baseh = layGroup.getHeight();
layGroup.getLocationOnScreen(new int[2]);
margl = (float) layoutParams.leftMargin;
margt = (float) layoutParams.topMargin;
break;
case 2:
int currx = x_cord;
int curry = y_cord;
float angle = (float) Math.toDegrees(Math.atan2((double) (curry - basey), (double) (currx - basex)));
if (angle < 0.0f) {
angle += 360.0f;
}
Log.d("rot", "BA: " + layGroup.getRotation() + " CA:" + angle);
int changesW = currx - basex;
int changesH = curry - basey;
changesW = (int) (Math.sqrt((double) ((changesW * changesW) + (changesH * changesH))) * Math.cos(Math.toRadians((double) (angle - layGroup.getRotation()))));
changesH = (int) (Math.sqrt((double) ((changesW * changesW) + (changesH * changesH))) * Math.sin(Math.toRadians((double) (angle - layGroup.getRotation()))));
int width = (changesW * 2) + basew;
int height = (changesH * 2) + baseh;
if ((width <= (layBg.getWidth() + layBg.getWidth() / 2) && textViewArt.getTextSize() >= 8.0f) && width > 150) {
layoutParams.width = width;
layoutParams.leftMargin = (int) (margl - ((float) changesW));
}
if ((height <= (layBg.getHeight() + layBg.getHeight() / 2) && textViewArt.getTextSize() >= 8.0f) && height > 150) {
layoutParams.height = height;
layoutParams.topMargin = (int) (margt - ((float) changesH));
}
layGroup.setLayoutParams(layoutParams);
break;
}
layGroup.invalidate();
return true;
}
}
public void flip() {
if (this.textViewArt.getRotationY() == 180.0f) {
this.textViewArt.setRotationY(0.0f);
} else {
this.textViewArt.setRotationY(180.0f);
}
}
class C0116GONE implements OnTouchListener {
C0116GONE() {
}
@SuppressLint({"NewApi"})
public boolean onTouch(View v, MotionEvent event) {
LayoutParams layoutParams = (LayoutParams) layGroup.getLayoutParams();
layBg = (RelativeLayout) getParent();
int[] pos = new int[2];
layBg.getLocationOnScreen(pos);
int x_cord = ((int) event.getRawX()) - pos[0];
int y_cord = ((int) event.getRawY()) - pos[1];
switch (event.getAction()) {
case 0:
startDegree = layGroup.getRotation();
pivx = layoutParams.leftMargin + (getWidth() / 2);
pivy = layoutParams.topMargin + (getHeight() / 2);
basex = x_cord - pivx;
basey = pivy - y_cord;
break;
case 2:
int curry = pivy - y_cord;
int angle = (int) (Math.toDegrees(Math.atan2((double) basey, (double) basex)) - Math.toDegrees(Math.atan2((double) curry, (double) (x_cord - pivx))));
if (angle < 0) {
angle += 360;
}
layGroup.setRotation((startDegree + ((float) angle)) % 360.0f);
break;
}
layGroup.invalidate();
return true;
}
}
class C01175 implements OnClickListener {
C01175() {
}
public void onClick(View v) {
layBg = (RelativeLayout) getParent();
layBg.performClick();
layBg.removeView(layGroup);
}
}
public int[] getScreenSizeInPixels() {
DisplayMetrics dm = new DisplayMetrics();
cntx.getWindowManager().getDefaultDisplay().getMetrics(dm);
return new int[]{dm.widthPixels, dm.heightPixels};
}
public TextArtView(Activity context, View parent) {
super(context);
cntx = context;
size = getScreenSizeInPixels();
layGroup = this;
basex = 0;
basey = 0;
pivx = 0;
pivy = 0;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.textart, this, true);
LayoutParams layoutParams = new LayoutParams(this.size[0] - (this.size[0] / 3), size[1] / 7);
layoutParams.leftMargin = parent.getWidth() / 2 - layoutParams.width / 2;
layoutParams.topMargin = parent.getHeight() / 2 - layoutParams.height / 2;
layGroup.setLayoutParams(layoutParams);
layBg = (RelativeLayout) getParent();
textViewArt = findViewById(R.id.textviewart);
textViewArt.setText(R.string.doubletaptoedit);
textViewArt.setGravity(17);
textViewArt.enableSizeCache(true);
textViewArt.setCursorVisible(false);
textViewArt.setTextSize(400.0f);
textViewArt.setTextColor(Color.WHITE);
textViewArt.setMinTextSize(8.0f);
btndel = (Button) findViewById(R.id.close);
btnrot = (Button) findViewById(R.id.rotate);
btnscl = (Button) findViewById(R.id.zoom);
btnflip = (Button) findViewById(R.id.flip);
imgring = (Button) findViewById(R.id.outring);
textViewArt.setOnCreateContextMenuListener(new C01121());
textViewArt.setOnTouchListener(new C011GONE2());
btnscl.setOnTouchListener(new C01153());
btnrot.setOnTouchListener(new C0116GONE());
btndel.setOnClickListener(new C01175());
btnflip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flip();
}
});
}
public void disableAll() {
btndel.setVisibility(GONE);
btnrot.setVisibility(GONE);
btnscl.setVisibility(GONE);
imgring.setVisibility(GONE);
btnflip.setVisibility(GONE);
textViewArt.setCursorVisible(false);
textViewArt.setSelected(false);
textViewArt.setFocusable(false);
textViewArt.setFocusableInTouchMode(false);
textViewArt.setClickable(false);
}
public void setTextGrevity(int g) {
textViewArt.setGravity(g);
}
public void setFreeze(boolean b) {
freeze = b;
}
public void setFont(Typeface tf) {
textViewArt.setTypeface(null, 0);
textViewArt.setTypeface(tf);
textViewArt.reAdjust();
}
public void setTextColor(int color) {
textViewArt.setTextColor(color);
textViewArt.reAdjust();
}
public void setStorkeColor(int color) {
textViewArt.setStroke(4, color);
}
public void setShadowColor(int color) {
textViewArt.addOuterShadow(6, 0, 8, color);
}
public TextPaint getPaint() {
return textViewArt.getPaint();
}
public void setGradient(int color1, int color2) {
float f = 0.0f;
float f2 = 0.0f;
textViewArt.getPaint().setShader(new LinearGradient(0.0f, f, f2, (float) textViewArt.getHeight(), new int[]{color1, color2}, new float[]{0.0f, 1.0f}, TileMode.CLAMP));
}
public String getText() {
return textViewArt.getText().toString();
}
public void setText() {
textViewArt.setText(R.string.doubletaptoedit);
}
public boolean isfrz() {
return freeze;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment