here we use a sample to use the gradient
float in[] = new float[] {0f,1f}; TextView textView= (TextView)findViewById(R.id.tv_test); Shader shader = new LinearGradient( 0, textView.getTextSize(), textView.getWidth(), textView.getHeight(), new int[]{Color.RED, Color.BLUE},in, Shader.TileMode.CLAMP); textView.getPaint().setShader(shader);
To achieve the second simple user Framelayout xml
<FrameLayout android:id="@+id/fl_test" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_test" android:layout_width="200dp" android:layout_height="100dp" android:background="@android:color/white" android:orientation="horizontal"> <View android:layout_width="wrap_content" android:layout_height="100dp" android:layout_weight="1" android:background="@android:color/holo_red_dark" /> <View android:layout_width="wrap_content" android:layout_height="100dp" android:layout_weight="1" android:background="@android:color/holo_blue_dark" /> </LinearLayout> <TextView android:id="@+id/tv_test2" android:layout_width="200dp" android:layout_height="100dp" android:layout_gravity="center" android:background="@android:color/transparent" android:gravity="center" android:text="A" android:textColor="@android:color/white" android:textSize="100sp" /> </FrameLayout>
For the third, you have to write a little and do some work at the pixel level
<TextView android:id="@+id/tv_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="H" android:textColor="@android:color/holo_red_dark" android:textSize="100sp" /> <FrameLayout android:id="@+id/fl_test" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_test" android:layout_width="200dp" android:layout_height="100dp" android:background="@android:color/white" android:orientation="horizontal"> <View android:layout_width="wrap_content" android:layout_height="100dp" android:layout_weight="1" android:background="@android:color/holo_red_dark" /> <View android:layout_width="wrap_content" android:layout_height="100dp" android:layout_weight="1" android:background="@android:color/holo_blue_dark" /> </LinearLayout> <TextView android:id="@+id/tv_test2" android:layout_width="200dp" android:layout_height="100dp" android:layout_gravity="center" android:background="@android:color/transparent" android:gravity="center" android:text="A" android:textColor="@android:color/white" android:textSize="100sp" /> </FrameLayout> <ImageView android:id="@+id/iv_test" android:layout_width="200dp" android:layout_height="200dp" />
public class MainActivity extends AppCompatActivity { View linearLayout; View tvTest2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); TextView textView = (TextView) findViewById(R.id.tv_test); Shader shader = new LinearGradient( 0, textView.getTextSize()/2, textView.getTextSize(), textView.getTextSize()/2,Color.RED, Color.BLUE, Shader.TileMode.CLAMP); textView.getPaint().setShader(shader); linearLayout = findViewById(R.id.ll_test); tvTest2 = findViewById(R.id.tv_test2); } @Override protected void onResume() { super.onResume(); new Handler().postDelayed(new Runnable() { @Override public void run() { ImageView imageView = (ImageView) findViewById(R.id.iv_test); Bitmap b1 = getBitmapFromView(linearLayout); Bitmap b2 = getBitmapFromView(tvTest2); imageView.setImageBitmap(textEffect(b1, b2)); } },2000); } public Bitmap textEffect(Bitmap image, Bitmap text) { if (image.getWidth() != text.getWidth() || image.getHeight() != text.getHeight()) { throw new IllegalArgumentException("Dimensions are not the same!"); } for (int y = 0; y < image.getHeight(); ++y) { for (int x = 0; x < image.getWidth(); ++x) { int textPixel = text.getPixel(x, y); int imagePixl = image.getPixel(x,y); int red = Color.red(textPixel); int blue = Color.blue(textPixel); int green = Color.green(textPixel); int alpha = Color.alpha(textPixel); Log.i("TAG", "textEffect: "+x+"-"+y+",-->"+red+","+blue+","+green+","+alpha); if (red == 255) { if (blue == 255) { if (green == 255) { image.setPixel(x, y, imagePixl); }else { image.setPixel(x, y, textPixel); } }else{ image.setPixel(x, y, textPixel); } }else { image.setPixel(x, y, textPixel); } } } return image; } public static Bitmap getBitmapFromView(View view) {
What you definitely want to achieve
First second
Third??
