Trước tiên tôi muốn nói xin vui lòng không đánh dấu câu hỏi này là trùng lặp. Nó khác với các bài viết khác theo một cách quan trọng. Có ai biết một cách mà tôi có thể chia màu sắc của mảnh thành hai màu sắc ngay xuống giữa? Cảm ơn mọi người!Cách chia Phân đoạn trong Android thành Hai Màu
Trả lời
tôi đạt được điều này bằng cách tạo nền lý tưởng trong photoshop, lưu nó dưới dạng .png
, và sau đó thiết lập đó làm nền hoạt động cho khi tôi cần nó. Đơn giản, sạch sẽ, dễ dàng, và bởi vì tôi chỉ cần nó là hai màu sắc tất cả các thời gian, tôi không phải lo lắng về hình ảnh trở thành pixelated.
đây là mẫu để sử dụng độ dốc
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);
Để đạt được thứ hai Đơn giản chỉ cần sử dụng 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>
Đối với một thứ ba bạn phải viết ít và làm một số công việc về mức độ điểm ảnh
<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);
/*Since text color is white so give the color of background to it
* else make it white*/
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) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
}
Điều bạn muốn đạt được là gì?
Đầu tiên một
Second một
một thứ ba ??
Xác định một TextView
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="O"
android:textColor="#88022b54"
android:textSize="250sp"
android:textStyle="bold"
android:shadowColor="#ff2e7cca"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="1"/>
Sau đó
Shader shader = new LinearGradient(
0, mytv.getTextSize()/2, mytv.getTextSize(), mytv.getTextSize()/2,
Color.RED, Color.BLUE,
Shader.TileMode.CLAMP);
mytv.getPaint().setShader(shader);
Tôi có thể áp dụng mã này cho một Phân đoạn không? – Ethan
Theo như tôi biết, không có vấn đề gì. Bạn đã thấy chạy chưa? Nó gần như những gì bạn muốn, phải không? – statosdotcom
Không, tôi không thể gửi cho tôi một liên kết? Bạn cũng có thể chỉnh sửa mã thành fragment_standard không? Nó cần phải được chia theo chiều dọc sao cho màu đỏ nằm ở nửa bên trái và màu xanh ở nửa bên phải – Ethan
Bạn có thể làm như thế này
String text = "<font color=#cc0029>Erste Farbe</font> <font color=#ffcc00>zweite Farbe</font>";
yourtextview.setText(Html.fromHtml(text));
- 1. Cách đặt Thẻ thành Phân đoạn trong Android
- 2. Chia ứng dụng Android thành hai phần
- 3. Phân đoạn hình ảnh màu xám
- 4. chia hình ảnh thành hai phần bằng cách sử dụng dải phân cách
- 5. Phân đoạn động trong Android
- 6. Chia hình ảnh thành các đoạn
- 7. tùy chỉnh màu cho phân đoạn được chọn trong điều khiển phân đoạn
- 8. Trợ giúp thuật toán: cách phân chia mảng thành N đoạn với phân đoạn lớn nhất có thể (phân đoạn cân bằng)
- 9. Chia td thành hai số
- 10. Hai màu đường ngang trong Android
- 11. Path() - phân đoạn màu thông minh
- 12. Android: Gọi lại AsyncTask thành Phân đoạn (Không hoạt động)
- 13. Phân đoạn cử chỉ trong Android
- 14. Cách triển khai hiển thị và ẩn đoạn bên trong phân đoạn trong android
- 15. Android - Đặt id phân đoạn
- 16. Android ICS: Xóa bộ chia màu xanh trong ActionBar?
- 17. Cách phân đoạn thứ hai chồng lên nhau trên đoạn đầu tiên trong khi transistion mảnh
- 18. Đoạn Android OnCreateView được gọi hai lần
- 19. cách sử dụng phân đoạn trong android 2.2?
- 20. mảng Chia thành hai mảng
- 21. Phân đoạn đường dẫn màu trong ggvis/layer_paths
- 22. Chuỗi chia cho hai dấu phân cách khác nhau
- 23. Chia hai số nguyên thành hai lần trong java
- 24. Phân chia mod của hai số nguyên
- 25. Xóa phân đoạn bộ nhớ chia sẻ với shmctl
- 26. Hợp nhất hai đoạn xml thành một?
- 27. Cách chia sẻ dữ liệu giữa hai diễn giả trong kiến trúc MVP trong Android?
- 28. Cách phân chia giữa hai chữ cái viết hoa?
- 29. chia thành hai sử dụng bash script
- 30. Phân đoạn phần tử chia sẻ chia sẻ không hoạt động với ViewPager
@Shark Tôi không nghĩ rằng Spannables có thể được sử dụng chia hiển thị của một chữ cái duy nhất. Bạn có thể thay đổi mọi thứ cho mỗi chữ cái, nhưng đó không phải là những gì OP hỏi. –
@ DougStevenson đủ công bằng, tôi đã không thực sự hiểu rõ những gì OP hỏi. Tôi vẫn không ... Ồ, tôi nghĩ mình đã hiểu rồi. Ví dụ: anh ta muốn một lá thư 'A' màu đỏ-đen là màu đỏ ở nửa bên trái và màu đen ở nửa bên phải. – Shark
Chúng ta có chắc đó là những gì OP hỏi? "phân chia màu của chế độ xem văn bản" cho tôi biết rằng nền văn bản của nó mà anh ta muốn chia làm hai, không phải là hiển thị chữ cái. Ông CÓ THỂ có nghĩa là, nhưng nó không rõ ràng. OP? –